Java -- Swing 组件使用

1. 示例1

Java -- Swing 组件使用

public class Main {

	JFrame f = new JFrame();
Icon okIcon = new ImageIcon("/home/test/start.png"); //图标文件
JButton ok = new JButton("OK", okIcon); JRadioButton male = new JRadioButton("man", true); //单选按键 group
JRadioButton female = new JRadioButton("woman", false);
ButtonGroup bg = new ButtonGroup(); JCheckBox married = new JCheckBox("married ?", false); String [] colors = new String[]{"red", "green", "blue"};
JComboBox<String> colorChooser = new JComboBox<String>(colors); //下拉选单,直接添加string[]
JList<String> colorList = new JList<String>(colors); JTextArea ta = new JTextArea(8, 20);
JTextField name = new JTextField(40);
JMenuBar mb = new JMenuBar(); //菜单栏
JMenu file = new JMenu("file"); //file菜单
JMenu edit = new JMenu("edit");
Icon newIcon = new ImageIcon("/home/test/start.png");
JMenuItem newItem = new JMenuItem("New", newIcon);
Icon saveIcon = new ImageIcon("/home/test/start.png");
JMenuItem saveItem = new JMenuItem("Save", saveIcon);
JMenuItem exitItem = new JMenuItem("Exit", newIcon);
JCheckBoxMenuItem autoWrap = new JCheckBoxMenuItem("AutoWrap");
JPopupMenu pop = new JPopupMenu();
ButtonGroup flavorGroup = new ButtonGroup();
JRadioButtonMenuItem metalItem = new JRadioButtonMenuItem("Metal style", true);
JRadioButtonMenuItem windowsItem = new JRadioButtonMenuItem("Windows style"); public void init()
{
JPanel bottom = new JPanel();
bottom.add(name);
bottom.add(ok);
f.add(bottom, BorderLayout.SOUTH); JPanel checkPanel = new JPanel();
checkPanel.add(colorChooser);
bg.add(male);
bg.add(female);
checkPanel.add(male);
checkPanel.add(female);
checkPanel.add(married);
Box topLeft = Box.createVerticalBox();
JScrollPane taJsp = new JScrollPane(ta);
topLeft.add(taJsp);
topLeft.add(checkPanel); Box top = Box.createHorizontalBox();
top.add(topLeft);
top.add(colorList);
f.add(top); newItem.setAccelerator(KeyStroke.getKeyStroke('N', InputEvent.CTRL_MASK)); //添加快捷键
newItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
ta.append("clicked newItem");
}
});
file.add(newItem);
file.add(saveItem);
file.add(exitItem);
edit.add(autoWrap);
edit.setToolTipText("autowrap");
edit.addSeparator();
mb.add(file);
mb.add(edit);
f.setJMenuBar(mb); //设置菜单栏 pop.add(metalItem); //可将这两个item添加到flavorGroup 形成单选
pop.add(windowsItem);
ActionListener flavorListener = new ActionListener() { // 右键单击选项事件
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
ta.append(e.getActionCommand());
}
};
metalItem.addActionListener(flavorListener);
windowsItem.addActionListener(flavorListener);
ta.setComponentPopupMenu(pop); //设置右键菜单 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置X窗口退出
f.pack();
f.setVisible(true);
} public static void main(String[] args) {
// TODO Auto-generated method stub
new Main().init();
}
}

2. 如上, name输入框中输入后 按Ok Button发送到 ta 中去。。。。为这个动作添加快捷键

Action sendMsg = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
ta.append(name.getText());
}
};
ok.addActionListener(sendMsg); //按键添加监听
name.getInputMap().put(KeyStroke.getKeyStroke('\n', java.awt.event.InputEvent.CTRL_MASK ),
"send"); //将Ctrl+Enter 键和"send"关联
name.getActionMap().put("send", sendMsg); //将"send"和 sendMsg Action 关联

3. JColorChooser 和 JFileChooser 。

4. 弹出提示框 showMessageDialog  showConfirmDialog  showInputDialog

public class Main {

	JFrame f = new JFrame();
JOptionPane option = new JOptionPane();
JButton bt1 = new JButton("bt1");
JButton bt2 = new JButton("bt2");
JButton bt3 = new JButton("bt3");
JButton bt4 = new JButton("bt4"); Icon icon = new ImageIcon("/home/test/start.png"); public void init()
{
bt1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) { //消息框
// TODO Auto-generated method stub
option.showMessageDialog(f, "show message", "test", JOptionPane.WARNING_MESSAGE, null);
// Message Type: ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, PLAIN_MESSAGE
}
}); bt2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) { //确认框
// TODO Auto-generated method stub
int chose = JOptionPane.showConfirmDialog(f, "show message", "test", JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, icon);
//Option Type: DEFAULT_OPTION, YES_NO_OPTION, YES_NO_CANCEL_OPTION, OK_CANCEL_OPTION
System.out.println(chose);
}
}); bt3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) { //输入框
// TODO Auto-generated method stub
String str = JOptionPane.showInputDialog(f, "message", "title", JOptionPane.WARNING_MESSAGE);
System.out.println(str);
}
}); f.add(bt1, BorderLayout.SOUTH);
f.add(bt2, BorderLayout.NORTH);
f.add(bt3, BorderLayout.EAST);
f.add(bt4, BorderLayout.WEST); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
} public static void main(String[] args) {
// TODO Auto-generated method stub
new Main().init();
}
}

5. JSplitPane 分割布局

Java -- Swing 组件使用

public class Main {

	JFrame f = new JFrame();
JLabel bookList = new JLabel("bookList");
JLabel bookCover = new JLabel("bookCover");
JLabel bookDesc = new JLabel("bookDesc"); public void init()
{
bookList.setPreferredSize(new Dimension(150, 300));
bookCover.setPreferredSize(new Dimension(300, 150));
bookDesc.setPreferredSize(new Dimension(300, 150)); JSplitPane left = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true,
bookCover, new JScrollPane(bookDesc)); //垂直分割
left.setOneTouchExpandable(true); //一处即展 功能
left.resetToPreferredSizes(); //
//left.setDividerSize(50); //设置分隔条大小
JSplitPane content = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
left, bookList); f.add(content);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
} public static void main(String[] args) {
// TODO Auto-generated method stub
new Main().init();
}
}

6. JTabbedPane 

 Java -- Swing 组件使用

public class Main {

	JFrame f = new JFrame();
JLabel bookList = new JLabel("bookList");
JLabel bookCover = new JLabel("bookCover");
JLabel bookDesc = new JLabel("bookDesc");
Icon icon = new ImageIcon("/home/test/start.png");
JTabbedPane tab = new JTabbedPane(JTabbedPane.LEFT, JTabbedPane.WRAP_TAB_LAYOUT); //定义tab,靠左 及 Tab排列策越 public void init()
{
bookList.setPreferredSize(new Dimension(150, 300));
bookCover.setPreferredSize(new Dimension(300, 150));
bookDesc.setPreferredSize(new Dimension(300, 150)); tab.addTab("title", icon, bookList, "bookList"); //添加Tab
tab.addTab("title", icon, bookCover, "bookCover");
tab.addTab("title", icon, bookDesc, "bookDesc");
tab.addChangeListener(new ChangeListener() { //Tab事件监听
@Override
public void stateChanged(ChangeEvent e) {
// TODO Auto-generated method stub
System.out.println(tab.getSelectedIndex());
}
}); f.add(tab);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
} public static void main(String[] args) {
// TODO Auto-generated method stub
new Main().init();
}
}

7. LayeredPane

Java -- Swing 组件使用

8. JDesktopPane   JInternalFrame

Java -- Swing 组件使用

public class Main {
JFrame f = new JFrame();
JDesktopPane desktop = new JDesktopPane(); public void init()
{
JInternalFrame iframe1 = new JInternalFrame("iframe1", true, true, true, true);
iframe1.reshape(20, 20, 300, 400);
iframe1.show();
JInternalFrame iframe2 = new JInternalFrame("iframe2", true, true, true, true);
iframe2.reshape(20, 20, 300, 400);
iframe2.show();
desktop.setPreferredSize(new Dimension(800, 600));
desktop.add(iframe1);
desktop.add(iframe2); f.add(desktop);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
} public static void main(String[] args) {
// TODO Auto-generated method stub
new Main().init();
}
}

9. 进度条

Java -- Swing 组件使用

public class Main {

	JFrame frame = new JFrame("Test ProgressBar");
JProgressBar bar = new JProgressBar(JProgressBar.VERTICAL );
JCheckBox indeterminate = new JCheckBox("indeterminate");
JCheckBox noBorder = new JCheckBox("noBorder");
JCheckBox dialogCheck = new JCheckBox("dialogCheck");
public void init()
{
final ProgressMonitor dialog = new ProgressMonitor(frame, "ProgressMonitor", "complete", 0, 100);
Box box = new Box(BoxLayout.Y_AXIS);
box.add(indeterminate);
box.add(noBorder);
box.add(dialogCheck);
frame.setLayout(new FlowLayout()); frame.add(box);
frame.add(bar); bar.setMinimum(0);
bar.setMaximum(100); bar.setStringPainted(true);
noBorder.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
bar.setBorderPainted(!noBorder.isSelected());
}
});
indeterminate.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
bar.setIndeterminate(indeterminate.isSelected());
bar.setStringPainted(!indeterminate.isSelected());
}
}); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true); for (int i = 0 ; i <= 100 ; i++)
{ bar.setValue(i);
dialog.setProgress(i);
try
{
Thread.sleep(100);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
public static void main(String[] args)
{
new Main().init();
}
}
上一篇:四则运算出题器(C++)-BUG修复


下一篇:java Swing组件和事件处理(二)