📄 imagebuttonsexample4.java
字号:
package JFCBook.Chapter2.jdk12;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class ImageButtonsExample4 extends JPanel {
public ImageButtonsExample4() {
setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
JPanel lowerPanel = new JPanel();
add(buttonPanel, BorderLayout.CENTER);
add(lowerPanel, BorderLayout.SOUTH);
buttonPanel.setLayout(new GridLayout(2, 1, 4, 4));
ok = new JButton("OK", new ImageIcon(getClass().getResource("images/ok_inactive.gif")));
cancel = new JButton("Cancel", new ImageIcon(getClass().getResource("images/cancel_inactive.gif")));
switchButton = new JButton("Switch orientation");
// Get the initial orientation
ComponentOrientation orientation = ok.getComponentOrientation();
isLTR = (orientation == null || orientation.isLeftToRight());
// Explicitly set text position of OK button
ok.setHorizontalTextPosition(SwingConstants.RIGHT);
// Specify locale-dependent alignment
ok.setHorizontalAlignment(SwingConstants.LEADING);
cancel.setHorizontalAlignment(SwingConstants.TRAILING);
ok.setFont(new Font("Dialog", Font.BOLD, 14));
cancel.setFont(new Font("Dialog", Font.BOLD, 14));
Icon okIcon = new ImageIcon(getClass().getResource("images/ok.gif"));
Icon cancelIcon = new ImageIcon(getClass().getResource("images/cancel.gif"));
ok.setPressedIcon(okIcon);
ok.setRolloverIcon(okIcon);
cancel.setPressedIcon(cancelIcon);
cancel.setRolloverIcon(cancelIcon);
buttonPanel.add(ok);
buttonPanel.add(cancel);
lowerPanel.add(switchButton);
switchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (isLTR) {
ok.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
cancel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
} else {
ok.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
cancel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
}
isLTR = !isLTR;
ok.repaint();
cancel.repaint();
}
});
}
public static void main(String[] args) {
JFrame f = new JFrame("Image Buttons 4");
f.getContentPane().add(new ImageButtonsExample4());
f.pack();
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
JButton ok;
JButton cancel;
JButton switchButton;
boolean isLTR = true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -