📄 exercise13_9.java
字号:
// Exercise13_9.java: Demonstrate button properties
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.border.*;
import java.net.URL;
public class Exercise13_9 extends JApplet {
// Create image icons
ImageIcon icon1;
// Generated by JBuilder
boolean isStandalone = false;
JPanel jpButtons = new JPanel();
JPanel jpSettings = new JPanel();
JPanel jpAlignment = new JPanel();
JButton jbt = new JButton();
TitledBorder titledBorder1;
TitledBorder titledBorder3;
GridLayout gridLayout3 = new GridLayout();
GridLayout gridLayout2 = new GridLayout();
JPanel jpHorizontalAlignment = new JPanel();
JPanel jpVerticalAlignment = new JPanel();
BorderLayout borderLayout1 = new BorderLayout();
TitledBorder titledBorder2;
BorderLayout borderLayout2 = new BorderLayout();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
BorderLayout borderLayout3 = new BorderLayout(5, 5);
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
GridLayout gridLayout1 = new GridLayout();
JComboBox jcboHoriAlignment = new JComboBox();
JComboBox jcboVertAlignment = new JComboBox();
GridLayout gridLayout5 = new GridLayout();
JPanel jPanel3 = new JPanel();
JPanel jPanel4 = new JPanel();
BorderLayout borderLayout4 = new BorderLayout(5, 5);
JLabel jLabel3 = new JLabel();
JLabel jLabel4 = new JLabel();
GridLayout gridLayout6 = new GridLayout();
JComboBox jcboHoriTextAlignment = new JComboBox();
JComboBox jcboVertTextAlignment = new JComboBox();
GridLayout gridLayout7 = new GridLayout();
// Construct the applet
public Exercise13_9() {
}
// Initialize the applet
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
// Component initialization
private void jbInit() throws Exception {
// Load images using URLs
Toolkit toolkit = Toolkit.getDefaultToolkit();
URL url = this.getClass().getResource("image/grapes.gif");
icon1 = new ImageIcon(toolkit.getImage(url));
// url = this.getClass().getResource("image/next.gif");
// url = this.getClass().getResource("image/home.gif");
// Generated by JBuilder
titledBorder1 = new TitledBorder("");
titledBorder3 = new TitledBorder("");
titledBorder2 = new TitledBorder("");
this.setSize(new Dimension(400,300));
this.getContentPane().setLayout(borderLayout1);
jpButtons.setLayout(borderLayout2);
jbt.setToolTipText("This is a JButton");
jbt.setIcon(icon1);
jbt.setText("Grapes");
titledBorder1.setTitle("Position Alignment");
jpAlignment.setLayout(gridLayout3);
titledBorder3.setTitle("Miscellanoues Options");
jpHorizontalAlignment.setLayout(borderLayout3);
jpHorizontalAlignment.setBorder(titledBorder1);
jpVerticalAlignment.setBorder(titledBorder2);
jpVerticalAlignment.setLayout(borderLayout4);
titledBorder2.setTitle("Text Position Alignment");
jLabel1.setText("Horizontal");
jLabel2.setText("Vertical ");
jPanel1.setLayout(gridLayout1);
gridLayout1.setRows(2);
jPanel2.setLayout(gridLayout5);
gridLayout5.setRows(2);
gridLayout5.setVgap(5);
jLabel3.setText("Horizontal");
jLabel4.setText("Vertical");
jPanel3.setLayout(gridLayout6);
gridLayout6.setRows(2);
jPanel4.setLayout(gridLayout7);
gridLayout7.setRows(2);
gridLayout7.setVgap(5);
jcboHoriAlignment.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jcboHoriAlignment_actionPerformed(e);
}
});
jcboVertAlignment.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jcboVertAlignment_actionPerformed(e);
}
});
jcboHoriTextAlignment.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jcboHoriTextAlignment_actionPerformed(e);
}
});
jcboVertTextAlignment.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jcboVertTextAlignment_actionPerformed(e);
}
});
this.getContentPane().add(jpButtons, BorderLayout.CENTER);
jpButtons.add(jbt, BorderLayout.CENTER);
this.getContentPane().add(jpSettings, BorderLayout.SOUTH);
jpSettings.setLayout(gridLayout2);
jpSettings.add(jpAlignment, null);
jpAlignment.add(jpHorizontalAlignment, null);
jpHorizontalAlignment.add(jPanel1, BorderLayout.WEST);
jPanel1.add(jLabel1, null);
jPanel1.add(jLabel2, null);
jpHorizontalAlignment.add(jPanel2, BorderLayout.CENTER);
jPanel2.add(jcboHoriAlignment, null);
jPanel2.add(jcboVertAlignment, null);
jpAlignment.add(jpVerticalAlignment, null);
jpVerticalAlignment.add(jPanel3, BorderLayout.WEST);
jPanel3.add(jLabel3, null);
jPanel3.add(jLabel4, null);
jpVerticalAlignment.add(jPanel4, BorderLayout.CENTER);
jPanel4.add(jcboHoriTextAlignment, null);
jPanel4.add(jcboVertTextAlignment, null);
// Add items to the combo boxes
jcboHoriAlignment.addItem("LEFT");
jcboHoriAlignment.addItem("CENTER");
jcboHoriAlignment.addItem("RIGHT");
jcboVertAlignment.addItem("TOP");
jcboVertAlignment.addItem("CENTER");
jcboVertAlignment.addItem("BOTTOM");
jcboHoriTextAlignment.addItem("LEFT");
jcboHoriTextAlignment.addItem("CENTER");
jcboHoriTextAlignment.addItem("RIGHT");
jcboVertTextAlignment.addItem("TOP");
jcboVertTextAlignment.addItem("CENTER");
jcboVertTextAlignment.addItem("BOTTOM");
}
// Main method
public static void main(String[] args) {
Exercise13_9 applet = new Exercise13_9();
applet.isStandalone = true;
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Exercise13_9");
frame.getContentPane().add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(400, 320);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2,
(d.height - frame.getSize().height) / 2);
frame.setVisible(true);
}
// Set horizontal alignment
private void setHorizontalAlignment(int alignment) {
jbt.setHorizontalAlignment(alignment);
}
// Set vertical alignment
private void setVerticalAlignment(int alignment) {
jbt.setVerticalAlignment(alignment);
}
void jcboHoriAlignment_actionPerformed(ActionEvent e) {
String selectedItem = (String)jcboHoriAlignment.getSelectedItem();
if (selectedItem.equals("LEFT"))
jbt.setHorizontalAlignment(JButton.LEFT);
if (selectedItem.equals("CENTER"))
jbt.setHorizontalAlignment(JButton.CENTER);
if (selectedItem.equals("RIGHT"))
jbt.setHorizontalAlignment(JButton.RIGHT);
}
void jcboVertAlignment_actionPerformed(ActionEvent e) {
String selectedItem = (String)jcboVertAlignment.getSelectedItem();
if (selectedItem.equals("TOP"))
jbt.setVerticalAlignment(JButton.TOP);
if (selectedItem.equals("CENTER"))
jbt.setVerticalAlignment(JButton.CENTER);
if (selectedItem.equals("BOTTOM"))
jbt.setVerticalAlignment(JButton.BOTTOM);
}
void jcboHoriTextAlignment_actionPerformed(ActionEvent e) {
String selectedItem = (String)jcboHoriTextAlignment.getSelectedItem();
if (selectedItem.equals("LEFT"))
jbt.setHorizontalTextPosition(JButton.LEFT);
if (selectedItem.equals("CENTER"))
jbt.setHorizontalTextPosition(JButton.CENTER);
if (selectedItem.equals("RIGHT"))
jbt.setHorizontalTextPosition(JButton.RIGHT);
}
void jcboVertTextAlignment_actionPerformed(ActionEvent e) {
String selectedItem = (String)jcboVertTextAlignment.getSelectedItem();
if (selectedItem.equals("TOP"))
jbt.setVerticalTextPosition(JButton.TOP);
if (selectedItem.equals("CENTER"))
jbt.setVerticalTextPosition(JButton.CENTER);
if (selectedItem.equals("BOTTOM"))
jbt.setVerticalTextPosition(JButton.BOTTOM);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -