import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Swing extends JFrame {
private JPanel panel1,panel2;
private JTabbedPane tabbedPane;
private JMenuBar menuBar;
private JMenu menuFile,menuHelp;
private JMenuItem itemNew,itemOpen,itemSave,itemExit,itemAbout,itemJava;
private JToolBar toolBar;
private JColorChooser colorChooser;
private JButton btnSave,btnPre,btnDown,btnSel,btnDel,
btnOk,btnMeg,btnOpt,btnInput,btnChange;
private JOptionPane optionPane;
private JDialog dialogColor;
public Swing(){
super("选项卡的使用");
tabbedPane = new JTabbedPane();
panel1 = new JPanel(new BorderLayout());
panel2 = new JPanel(new FlowLayout());
tabbedPane.add("选项卡1",panel1);
tabbedPane.add("选项卡2",panel2);
this.add(tabbedPane);
// 菜单栏
menuBar = new JMenuBar();
menuFile = new JMenu("文件");
menuHelp = new JMenu("帮助");
itemNew = new JMenuItem("新建");
itemOpen = new JMenuItem("打开");
itemSave = new JMenuItem("保存");
itemExit = new JMenuItem("退出");
itemAbout = new JMenuItem("关于");
itemJava = new JMenuItem("Java工程");
menuBar.add(menuFile);
menuBar.add(menuHelp);
menuFile.add(itemNew);
menuFile.add(itemOpen);
menuFile.add(itemSave);
menuFile.addSeparator();
menuFile.add(itemExit);
menuHelp.add(itemAbout);
menuHelp.add(itemJava);
this.setJMenuBar(menuBar); // 给窗体设置菜单栏
// 工具栏
toolBar = new JToolBar();
btnSave = new JButton("保存",new ImageIcon("images/5.jpg"));
btnPre = new JButton("预览",new ImageIcon("images/3.jpg"));
btnDown = new JButton("下载",new ImageIcon("images/4.jpg"));
btnSel = new JButton("查询",new ImageIcon("images/2.jpg"));
btnDel = new JButton("删除",new ImageIcon("images/1.jpg"));
btnSave.setToolTipText("保存");
btnPre.setToolTipText("预览");
btnDown.setToolTipText("下载");
btnSel.setToolTipText("查询");
btnDel.setToolTipText("删除");
toolBar.add(btnSave);
toolBar.add(btnPre);
toolBar.add(btnDown);
toolBar.add(btnSel);
toolBar.add(btnDel);
this.add(toolBar, BorderLayout.NORTH); // 将工具栏添加到窗体的顶部
btnOk = new JButton("确认");
btnMeg = new JButton("消息");
btnOpt = new JButton("选项");
btnInput = new JButton("输入");
panel1.add(btnOk,BorderLayout.WEST);
panel1.add(btnMeg, BorderLayout.SOUTH);
panel1.add(btnOpt,BorderLayout.EAST);
panel1.add(btnInput,BorderLayout.NORTH);
btnOk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int val = JOptionPane.showConfirmDialog(btnOk,"您确定要删除吗?",
"确认框",JOptionPane.YES_NO_OPTION);
if(val == JOptionPane.YES_OPTION){
System.out.println("删除成功!");
}else{
System.out.println("删除失败!");
}
}
});
btnMeg.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(btnMeg,"今晚睡觉前要完成这个实验报告!",
"消息框",JOptionPane.INFORMATION_MESSAGE);
System.out.println("收到!");
}
});
btnOpt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Object[] objects = {"red","blue","yellow"};
int val = JOptionPane.showOptionDialog(btnOpt,"选择一种你喜欢的颜色",
"选项框",JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE,
null,objects,objects[1]);
if(val != JOptionPane.CLOSED_OPTION){
System.out.println("你选择的颜色:"+objects[val]);
}
}
});
btnInput.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String val = JOptionPane.showInputDialog(btnInput,"请在框框输入内容:",
"输入框",JOptionPane.YES_NO_OPTION);
System.out.println("你输入的内容是:"+val);
}
});
colorChooser = new JColorChooser();
dialogColor = JColorChooser.createDialog(this,"颜色面板",true,colorChooser,
null,null);
btnChange = new JButton("改变面板背景颜色");
panel2.add(btnChange);
btnChange.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dialogColor.setVisible(true);
panel2.setBackground(colorChooser.getColor());
}
});
this.setSize(400,500);
this.setLocation(200,100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new Swing();
}
}
Java程序设计——JTabbedPane选项卡面板(Swing UI )
于 2022-07-08 19:35:13 首次发布