文章目录
- java awt,swing图形化界面代码案例合集
java awt,swing图形化界面代码案例合集
package Demo1;
import java.awt.*;
public class Demo1 {
public static void main(String[] args) {
Frame frame = new Frame("这里测试windows窗口");
frame.setLocation(100,100);
frame.setSize(500,300);
frame.setVisible(true);
}
}
package Demo1;
import java.awt.*;
public class Demo2 {
public static void main(String[] args) {
Frame frame = new Frame("计算器");
Panel p = new Panel();
p.add(new TextField(30));
frame.add(p,BorderLayout.NORTH);
Panel p2 = new Panel();
p2.setLayout(new GridLayout(3,3,4,4));
for (int i = 0; i < 10; i++) {
p2.add(new Button(i+""));
}
p2.add(new Button("+"));
p2.add(new Button("-"));
p2.add(new Button("*"));
p2.add(new Button("/"));
frame.add(p2);
frame.pack();
frame.setVisible(true);
}
}
package Demo1;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.nio.charset.UnmappableCharacterException;
public class Demo3 {
public static void main(String[] args) {
Frame frame = new Frame("这里测试cardlayout");
Panel p1 = new Panel();
CardLayout cardLayout = new CardLayout();
p1.setLayout(cardLayout);
String[] names ={"A","B","C","D","E"};
for (int i = 0; i <names.length; i++) {
p1.add(names[i],new Button(names[i]));
}
frame.add(p1);
Panel p2 = new Panel();
Button b1 = new Button("privious");
Button b2 = new Button("next");
Button b3 = new Button("first");
Button b4 = new Button("last");
Button b5 = new Button("third");
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
switch (actionCommand){
case"privious":
cardLayout.previous(p1);
break;
case"next":
cardLayout.next(p1);
break;
case"first":
cardLayout.first(p1);
break;
case"last":
cardLayout.last(p1);
break;
case "third":
cardLayout.show(p1,"C");
break;
}
}
};
b1.addActionListener(listener);
b2.addActionListener(listener);
b3.addActionListener(listener);
b4.addActionListener(listener);
b5.addActionListener(listener);
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b4);
p2.add(b5);
frame.add(p2,BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
}
package Demo1;
import javax.swing.*;
import java.awt.*;
public class Demo4 {
public static void main(String[] args){
Frame frame = new Frame();
BoxLayout boxlayout = new BoxLayout(frame,BoxLayout.X_AXIS);
frame.setLayout(boxlayout);
frame.add(new Button("button1"));
frame.add(new Button("button2"));
frame.pack();
frame.setVisible(true);
}
}
package Demo1;
import javax.swing.*;
import java.awt.*;
public class Demo5 {
public static void main(String[] args) {
Frame frame = new Frame("test");
Box b1 = Box.createHorizontalBox();
b1.add(new Button("button1"));
b1.add(new Button("button2"));
Box b2 = Box.createVerticalBox();
b2.add(new Button("buttonA"));
b2.add(new Button("buttonB"));
frame.add(b1,BorderLayout.SOUTH);
frame.add(b2,BorderLayout.NORTH);
frame.pack();
frame.setVisible(true);
}
}
package Demo1;
import javax.swing.*;
import java.awt.*;
public class Demo6 {
Frame frame = new Frame("这里测试基本组件");
Button ok = new Button("确认");
CheckboxGroup cbg = new CheckboxGroup();
Checkbox male = new Checkbox("男", cbg, true);
Checkbox female = new Checkbox("女", cbg, false);
Checkbox married = new Checkbox("是否已婚?", false);
Choice colorChooser = new Choice();
List colorList = new List(6, true);
TextArea ta = new TextArea(5, 20);
TextField tf = new TextField(50);
public void init() {
colorChooser.add("红色");
colorChooser.add("绿色");
colorChooser.add("蓝色");
colorList.add("红色");
colorList.add("绿色");
colorList.add("蓝色");
Panel bottom = new Panel();
bottom.add(tf);
bottom.add(ok);
frame.add(bottom,BorderLayout.SOUTH);
Panel checkPanel = new Panel();
checkPanel.add(colorChooser);
checkPanel.add(male);
checkPanel.add(female);
checkPanel.add(married);
Box topLeft = Box.createVerticalBox();
topLeft.add(ta);
topLeft.add(checkPanel);
Box top = Box.createHorizontalBox();
top.add(topLeft);
top.add(colorList);
frame.add(top);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new Demo6().init();
}
}
package Demo1;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Demo7 {
Frame frame =new Frame("test");
TextField tf = new TextField("30");
Button ok = new Button("ok");
public void init(){
ok.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
tf.setText("hello pofen");
}
});
frame.add(tf,BorderLayout.NORTH);
frame.add(ok,BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new Demo7().init();
}
}
package Demo1;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
public class Demo8 {
private Frame frame = new Frame("test pofen");
MenuBar menuBar = new MenuBar();
Menu filemenu = new Menu("文件");
Menu editMenu = new Menu("编辑");
Menu formatMenu = new Menu("格式");
MenuItem auto = new MenuItem("自动换行");
MenuItem copy =new MenuItem("复制");
MenuItem paste =new MenuItem("粘贴");
MenuItem comment =new MenuItem("注释 ctrl+shift+q",new MenuShortcut(KeyEvent.VK_Q,
true));
MenuItem canceComment = new MenuItem("取消注释");
TextArea textArea = new TextArea(6,40);
public void init(){
comment.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.print("ok : ");
textArea.append("你点了菜单项 "+ e.getActionCommand());
}
});
formatMenu.add(comment);
formatMenu.add(canceComment);
editMenu.add(auto);
editMenu.add(copy);
editMenu.add(paste);
editMenu.add(formatMenu);
menuBar.add(filemenu);
menuBar.add(editMenu);
menuBar.add(filemenu);
frame.setMenuBar(menuBar);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new Demo8().init();
}
}
package Demo1;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
public class Demo9 {
private Frame frame = new Frame("test pofen");
MenuBar menuBar = new MenuBar();
Menu filemenu = new Menu("文件");
Menu editMenu = new Menu("编辑");
Menu formatMenu = new Menu("格式");
MenuItem auto = new MenuItem("自动换行");
MenuItem copy =new MenuItem("复制");
MenuItem paste =new MenuItem("粘贴");
MenuItem comment =new MenuItem("注释 ",new MenuShortcut(KeyEvent.VK_Q,
true));
MenuItem canceComment = new MenuItem("取消注释");
TextArea textArea = new TextArea(6,40);
public void init(){
comment.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.print("ok : ");
textArea.append("你点了菜单项 "+ e.getActionCommand()+"\n");
}
});
formatMenu.add(comment);
formatMenu.add(canceComment);
editMenu.add(auto);
editMenu.add(copy);
editMenu.add(paste);
editMenu.add(formatMenu);
menuBar.add(filemenu);
menuBar.add(editMenu);
frame.setMenuBar(menuBar);
frame.add(textArea);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new Demo9().init();
}
}
package Demo1;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Demo10 {
Frame frame =new Frame("这里测试绘图");
private final String RECT_SHAPE="rect";
private final String OVAL_SHAPE="oval";
Button buttonReact = new Button("绘制矩形");
Button buttonOval = new Button("绘制椭圆");
private String shape = "";
private class MyCanvas extends Canvas{
@Override
public void paint(Graphics g){
if(shape.equals(RECT_SHAPE)){
g.setColor(Color.black);
g.drawRect(100,100,150,100);
}else if(shape.equals(OVAL_SHAPE)){
g.setColor(Color.RED);
g.drawOval(100,100,150,100);
}
}
}
MyCanvas drawArea = new MyCanvas();
public void init(){
buttonReact.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
shape = RECT_SHAPE;
drawArea.repaint();
}
});
buttonOval.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
shape = OVAL_SHAPE;
drawArea.repaint();
}
});
Panel p = new Panel();
p.add(buttonOval);
p.add(buttonReact);
frame.add(p,BorderLayout.SOUTH);
drawArea.setPreferredSize(new Dimension(300,300));
frame.add(drawArea);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new Demo10().init();
}
}
package Demo1;
import javax.security.auth.kerberos.KerberosKey;
import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;
public class Demo11 {
int sum = -1;
private Frame frame = new Frame("弹球游戏");
private final int TABLE_WIDTH = 400;
private final int TABLE_HEIGHT = 400;
private final int RACKET_WIDTH = 60;
private final int RACKET_HEIGHT = 10;
private final int BALL_SIZE = 16;
private int ballX = 120;
private int ballY = 20;
private int speedY = 10;
private int speedX = 5;
private int racketX = 120;
private int racketY = 340;
private boolean isOver = false;
private Timer timer;
private class myCanvas extends Canvas {
@Override
public void paint(Graphics g) {
if (isOver) {
g.setColor(Color.BLUE);
g.setFont(new Font("Timew", Font.BOLD, 30));
g.drawString("游戏结束!,你一共成功了"+sum+"次", 50, 200);
} else {
g.setColor(Color.RED);
g.fillOval(ballX, ballY, BALL_SIZE, BALL_SIZE);
g.setColor(Color.PINK);
g.fillRect(racketX, racketY, RACKET_WIDTH, RACKET_HEIGHT);
}
}
}
myCanvas drawArea = new myCanvas();
public void init() {
KeyListener keyListener = new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_LEFT) {
if (racketX >= 10) {
racketX -= 20;
}
}
if (keyCode == KeyEvent.VK_RIGHT) {
if (racketX < TABLE_WIDTH - RACKET_WIDTH) {
racketX += 20;
}
}
if (keyCode == KeyEvent.VK_UP) {
racketY -= 20;
}
}
};
frame.addKeyListener(keyListener);
drawArea.addKeyListener(keyListener);
ActionListener task = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(ballX<=0||ballX>=TABLE_WIDTH+BALL_SIZE){
speedX=-speedX;
}
if(ballY<=0||(ballY>=racketY-BALL_SIZE&&ballX>racketX&&ballX<racketX+RACKET_WIDTH)){
speedY=-speedY;
sum = sum+1;
}
if(ballY>racketY-BALL_SIZE&&(ballX<racketX || ballX> racketX+RACKET_WIDTH)){
timer.stop();
isOver=true;
drawArea.repaint();
}
ballX-=speedX;
ballY+=speedY;
drawArea.repaint();
}
};
timer = new Timer(100,task);
timer.start();
drawArea.setPreferredSize(new Dimension(TABLE_WIDTH,TABLE_HEIGHT));
frame.add((drawArea));
frame.pack();
frame.setVisible(true);
}
public static void main(String[]args){
new Demo11().init();
}
}
package Demo1;
import javax.swing.*;
import java.awt.*;
public class Demo12 {
JFrame f = new JFrame("Swing基本组件的测试");
JMenuBar menuBar = new JMenuBar();
JMenu fileMenue = new JMenu("文件");
JMenu editMenue = new JMenu("编辑");
JMenuItem auto = new JMenuItem("自动换行");
JMenuItem copy = new JMenuItem("复制");
JMenuItem paste = new JMenuItem("粘贴");
JMenu formatMenu = new JMenu("格式");
JMenuItem comment = new JMenuItem("注释");
JMenuItem cancelcomment = new JMenuItem("取消注释");
JTextArea ta = new JTextArea(8,20);
String[] colorsList ={"红色","绿色","蓝色"};
JList<String> colorList = new JList<String>(colorsList);
JComboBox<String> colorSelect = new JComboBox<String>();
ButtonGroup bg = new ButtonGroup();
JRadioButton male = new JRadioButton("男",false);
JRadioButton female = new JRadioButton("男",true);
JCheckBox isMarried = new JCheckBox("是否已婚?",true);
JTextField tf = new JTextField(40);
JButton ok = new JButton("确定",new ImageIcon("Code2\\img\\1.jpg"));
JPopupMenu jPopupMenu = new JPopupMenu();
ButtonGroup poupButtonBg = new ButtonGroup();
JRadioButtonMenuItem metalItem = new JRadioButtonMenuItem("metal 风格");
JRadioButtonMenuItem nimbusItem = new JRadioButtonMenuItem("nimbus 风格");
JRadioButtonMenuItem wiodowsItem = new JRadioButtonMenuItem("wiodows 风格");
JRadioButtonMenuItem wiodowsClassItem = new JRadioButtonMenuItem("wiodows 经典风格");
JRadioButtonMenuItem motifItem = new JRadioButtonMenuItem("mmotif 风格");
public void init(){
JPanel bottonPanel = new JPanel();
bottonPanel.add(tf);
bottonPanel.add(ok);
f.add(bottonPanel,BorderLayout.SOUTH);
JPanel selectPanle = new JPanel();
colorSelect.addItem("红色");
colorSelect.addItem("绿色");
colorSelect.addItem("蓝色");
selectPanle.add(colorSelect);
bg.add(male);
bg.add(female);
selectPanle.add(male);
selectPanle.add(female);
selectPanle.add(isMarried);
Box topleft = Box.createVerticalBox();
topleft.add(ta);
topleft.add(selectPanle);
Box top = Box.createHorizontalBox();
top.add(topleft);
top.add(colorList);
f.add(top);
formatMenu.add(comment);
formatMenu.add(cancelcomment);
editMenue.add(auto);
editMenue.addSeparator();
editMenue.add(formatMenu);
editMenue.add(copy);
editMenue.add(paste);
menuBar.add(fileMenue);
menuBar.add(editMenue);
f.setJMenuBar(menuBar);
poupButtonBg.add(metalItem);
poupButtonBg.add(nimbusItem);
poupButtonBg.add(wiodowsItem);
poupButtonBg.add(wiodowsClassItem);
poupButtonBg.add(motifItem);
poupButtonBg.add(metalItem);
ta.setComponentPopupMenu(jPopupMenu);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
new Demo12().init();
}
}
package Demo1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
public class Demo13 {
JFrame jf = new JFrame("测试工具条");
JTextArea jta = new JTextArea(6,3);
JToolBar jToolBar = new JToolBar("播放工具条",SwingConstants.HORIZONTAL);
Action pre = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
jta.append("上一曲.\n");
}
};
Action pause = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
jta.append("暂停.\n");
}
};
Action next = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
jta.append("下一曲.\n");
}
};
public void init(){
JButton prebtn = new JButton(pre);
JButton pausebtn = new JButton(pause);
JButton nextbtn = new JButton(next);
jToolBar.add(prebtn);
jToolBar.addSeparator();
jToolBar.add(pausebtn);
jToolBar.addSeparator();
jToolBar.add(nextbtn);
jToolBar.setFloatable(true);
jf.add(jToolBar,BorderLayout.NORTH);
jf.add(jta);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.pack();
jf.setVisible(true);
}
public static void main(String[] args) {
new Demo13().init();
}
}
package Demo1;
import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class Demo14 {
JFrame jFrame = new JFrame("测试颜色选择器");
JTextArea jta = new JTextArea("我是pofen",6,30);
JButton btn = new JButton(new AbstractAction("change text color") {
@Override
public void actionPerformed(ActionEvent e) {
Color result = JColorChooser.showDialog(jFrame,"颜色选择器",Color.white);
jta.setBackground(result);
}
});
public void init(){
jFrame.add(jta);
jFrame.add(btn,BorderLayout.SOUTH);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.pack();
jFrame.setVisible(true);
}
public static void main(String[] args) {
new Demo14().init();
}
}
package Demo1;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class Demo15 {
JFrame jf = new JFrame("文件选择器");
JMenuBar jmb = new JMenuBar();
JMenu jmenu = new JMenu("file");
JMenuItem open = new JMenuItem(new AbstractAction("打开") {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser(".");
fileChooser.showOpenDialog(jf);
File file = fileChooser.getSelectedFile();
try {
image = ImageIO.read(file);
drawArea.repaint();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
});
JMenuItem save = new JMenuItem(new AbstractAction("protect") {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser(".");
fileChooser.showOpenDialog(jf);
File file = fileChooser.getSelectedFile();
try {
ImageIO.write(image,"jpeg",file);
} catch (IOException ioException) {
ioException.printStackTrace();
}
try {
image = ImageIO.read(file);
drawArea.repaint();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
});
BufferedImage image;
private class MyCanvas extends JPanel{
@Override
public void paint(Graphics g) {
g.drawImage(image,0,0,null);
}
}
MyCanvas drawArea = new MyCanvas();
public void init(){
jmenu.add(open);
jmenu.add(save);
jmb.add(jmenu);
jf.setJMenuBar(jmb);
drawArea.setPreferredSize(new Dimension(740,500));
jf.add(drawArea);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.pack();
jf.setVisible(true);
}
public static void main(String[] args) {
new Demo15().init();
}
}
package Demo1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
public class Demo16 {
JFrame jf = new JFrame("测试消息对话框");
JTextArea jta = new JTextArea(6,30);
JButton btn = new JButton(new AbstractAction("弹出消息对话框") {
@Override
public void actionPerformed(ActionEvent e) {
String text = " 你输入了 :"+jta.getText();
JOptionPane.showMessageDialog(jf,text,"消息对话框",JOptionPane.PLAIN_MESSAGE);
}
});
public void init(){
jf.add(jta);
jf.add(btn, BorderLayout.SOUTH);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.pack();
jf.setVisible(true);
}
public static void main(String[] args) {
new Demo16().init();
}
}
package Demo1;
import com.sun.jdi.PathSearchingVirtualMachine;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
public class Demo17 {
JFrame jf = new JFrame("测试确认对话框");
JTextArea jta = new JTextArea(15,30);
JButton btn = new JButton(new AbstractAction("弹出确认消息对话框") {
@Override
public void actionPerformed(ActionEvent e) {
String text = jta.getText();
int result = JOptionPane.showConfirmDialog(jf,text,"确认对话框",JOptionPane.YES_NO_CANCEL_OPTION);
jta.append("\n");
if(result==JOptionPane.YES_OPTION){
jta.append("用户点击了 是 按钮\n");
}
if(result==JOptionPane.NO_OPTION){
jta.append("用户点击了 否 按钮\n");
}
if(result==JOptionPane.CANCEL_OPTION){
jta.append("用户点击了 取消 按钮\n");
}
}
});
public void init(){
jf.add(jta);
jf.add(btn, BorderLayout.SOUTH);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.pack();
jf.setVisible(true);
}
public static void main(String[] args) {
new Demo17().init();
}
}
package Demo1;
import com.sun.jdi.PathSearchingVirtualMachine;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
public class Demo18 {
JFrame jf = new JFrame("测试输入对话框");
JTextArea jta = new JTextArea(15,30);
JButton btn = new JButton(new AbstractAction("弹出输入消息对话框") {
@Override
public void actionPerformed(ActionEvent e) {
String result = JOptionPane.showInputDialog(jf, "请填写你的银行卡账号", "输入对话框", JOptionPane.INFORMATION_MESSAGE);
jta.append(result);
}
});
public void init(){
jf.add(jta);
jf.add(btn, BorderLayout.SOUTH);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.pack();
jf.setVisible(true);
}
public static void main(String[] args) {
new Demo18().init();
}
}
package Demo1;
import com.sun.jdi.PathSearchingVirtualMachine;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
public class Demo19 {
JFrame jf = new JFrame("测试选项对话框");
JTextArea jta = new JTextArea(15,30);
JButton btn = new JButton(new AbstractAction("弹出选项消息对话框") {
@Override
public void actionPerformed(ActionEvent e) {
jta.append("\n");
int result =JOptionPane.showOptionDialog(jf,"选择尿不湿号码","选项对话框",JOptionPane.DEFAULT_OPTION,
JOptionPane.INFORMATION_MESSAGE,null,new String[]{"大号","中号","小号"},"中号");
switch (result){
case 0:
jta.append("用户选择了大号");
break;
case 1 :
jta.append("用户选择了中号");
break;
case 2 :
jta.append("用户选择了小号");
break;
}
}
});
public void init(){
jf.add(jta);
jf.add(btn, BorderLayout.SOUTH);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.pack();
jf.setVisible(true);
}
public static void main(String[] args) {
new Demo19().init();
}
}
package Demo1;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.plaf.basic.BasicButtonListener;
public class Demo20 {
int i =0;
JFrame jf = new JFrame("测试JTabledPane");
JTabbedPane jTabbedPane = new JTabbedPane(SwingConstants.LEFT,JTabbedPane.SCROLL_TAB_LAYOUT);
public void init(){
jTabbedPane.addTab("用户管理",new ImageIcon("\\img\\1.jpg"),new JList<String>(new String[]{"商品一","商品二","商品三"}));
jTabbedPane.addTab("用户管理",new JList<String>(new String[]{"用户一","用户二","用户三"}));
jTabbedPane.addTab("用户管理",new ImageIcon("\\img\\2.jpg"),new JList<String>(new String[]{"订单一","订单二","订单三","订单4"}));
jTabbedPane.setEnabledAt(0,false);
jTabbedPane.setSelectedIndex(1);
jTabbedPane.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
int selectedIndex=jTabbedPane.getSelectedIndex();
JOptionPane.showMessageDialog(jf,"当前选中了第:"+(selectedIndex+1)+"个标签");
i++;
System.out.println("ok pofen"+i);
}
});
jf.add(jTabbedPane);
jf.setBounds(400,400,400,400);
jf.setResizable(false);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
public static void main(String[] args){
new Demo20().init();
}
}
package Demo1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Demo21 {
JFrame jf = new JFrame("测试进度条");
JCheckBox indeterminate = new JCheckBox("不确定进度");
JCheckBox noBorder= new JCheckBox("不绘制边框");
JProgressBar bar = new JProgressBar(JProgressBar.HORIZONTAL,0,100);
public void init(){
indeterminate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
boolean select = indeterminate.isSelected();
bar.setIndeterminate(select);
bar.setStringPainted(!select);
}
});
noBorder.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
boolean selected = noBorder.isSelected();
bar.setStringPainted(!selected);
}
});
Box box = Box.createVerticalBox();
box.add(indeterminate);
box.add(noBorder);
bar.setStringPainted(true);
bar.setBorderPainted(true);
jf.setLayout(new FlowLayout());
jf.add(box);
jf.add(bar);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.pack();
jf.setVisible(true);
for (int i = 0; i <=100; i++) {
bar.setValue(i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new Demo21().init();
}
}
package Demo1;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.List;
import java.util.Vector;
public class Demo22 {
JFrame jf =new JFrame("列表测试框");
String[] books = {"java自学宝典","轻量级javaee企业应用实战","Android基础教程",
"Jquery实战教程","spriingboot企业级开发"};
JPanel layoutPanel = new JPanel();
ButtonGroup layoutgroup = new ButtonGroup();
JPanel selectModelPanel = new JPanel();
ButtonGroup selectModeGroup = new ButtonGroup();
JTextArea favorite= new JTextArea(4,40);
JList<String > bookList;
JComboBox<String> bookSelector;
public void init(){
bookList = new JList<>(books);
addBtn2layoutPanel("纵向滚动",JList.VERTICAL);
addBtn2layoutPanel("纵向滚动",JList.VERTICAL_WRAP);
addBtn2layoutPanel("纵向滚动",JList.HORIZONTAL_WRAP);
addBtn2SelectPanel("无限制",ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
addBtn2SelectPanel("单选",ListSelectionModel.SINGLE_SELECTION);
addBtn2SelectPanel("单范围",ListSelectionModel.SINGLE_INTERVAL_SELECTION);
bookList.setVisibleRowCount(3);
bookList.setSelectionInterval(2,4);
bookList.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
List<String> selectdValueList = bookList.getSelectedValuesList();
favorite.setText("");
for(String str: selectdValueList){
favorite.append(str+"\n");
}
}
});
Box booklistVBox =Box.createVerticalBox();
booklistVBox.add(new JScrollPane(bookList));
booklistVBox.add(layoutPanel);
booklistVBox.add(selectModelPanel);
Vector<String> vector = new Vector<>();
List<String> list = List.of("java自学宝典","轻量级javaee企业应用实战","Android基础教程",
"Jquery实战教程","spriingboot企业级开发");
vector.addAll(list);
bookSelector = new JComboBox<>(vector);
bookSelector.setEditable(true);
bookSelector.setMaximumRowCount(5);
bookSelector.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
Object selectedItem = bookSelector.getSelectedItem();
favorite.setText(selectedItem.toString());
}
});
Box topBox = Box.createHorizontalBox();
topBox.add(booklistVBox);
JPanel booksSelectPanel = new JPanel();
booksSelectPanel.add(bookSelector);
topBox.add(booksSelectPanel);
JPanel bottonPanel = new JPanel();
bottonPanel.setLayout(new BorderLayout());
bottonPanel.add(new JLabel("你追喜欢的书是"),BorderLayout.NORTH);
bottonPanel.add(favorite);
Box holeBox = Box.createVerticalBox();
holeBox.add(topBox);
holeBox.add(bottonPanel);
jf.add(holeBox);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.pack();
jf.setVisible(true);
}
public void addBtn2layoutPanel(String name , int layoutType){
layoutPanel.setBorder(new TitledBorder(new EtchedBorder(),"确定选项布局"));
JRadioButton button = new JRadioButton(name);
layoutPanel.add(button);
if(layoutgroup.getButtonCount()==0){
button.setSelected(true);
}
layoutgroup.add(button);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
bookList.setLayoutOrientation(layoutType);
}
});
}
public void addBtn2SelectPanel(String name , int selectModel){
selectModelPanel.setBorder(new TitledBorder(new EtchedBorder(),"确定选项布局"));
JRadioButton button = new JRadioButton(name);
layoutPanel.add(button);
if(selectModeGroup.getButtonCount()==0){
button.setSelected(true);
}
selectModelPanel.add(button);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
bookList.setSelectionMode(selectModel);
}
});
}
public static void main(String[] args) {
new Demo22().init();
}
}
package Demo1;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
public class Demo23 {
JFrame jf = new JFrame("简单数");
public void init() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("中国");
DefaultMutableTreeNode guangdong = new DefaultMutableTreeNode("广东");
DefaultMutableTreeNode guanxi = new DefaultMutableTreeNode("广西");
DefaultMutableTreeNode foshan = new DefaultMutableTreeNode("佛山");
DefaultMutableTreeNode shantou = new DefaultMutableTreeNode("汕头");
DefaultMutableTreeNode guilin = new DefaultMutableTreeNode("桂林");
DefaultMutableTreeNode nanling = new DefaultMutableTreeNode("南宁");
JButton addSiblingBtn = new JButton("添加兄弟结点");
JButton addChildBtn = new JButton("添加子节点");
JButton deletBtn = new JButton("删除结点");
JButton editBtn = new JButton("编辑当前结点");
root.add(guangdong);
root.add(guanxi);
guangdong.add(foshan);
guangdong.add(shantou);
guanxi.add(guilin);
guanxi.add(nanling);
JTree tree = new JTree(root);
tree.setEditable(true);
DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
jf.add(tree);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.pack();
jf.setVisible(true);
}
public static void main(String[] args) {
new Demo23().init();
}
}
package Demo1;
import javax.swing.*;
import javax.swing.table.TableColumn;
public class Demo24 {
JFrame jf = new JFrame("简单表格");
Object[] titles = {"姓名","年龄","性别"};
Object[][] data = {
{"李清照","女","20"},
{"苏格拉底","男","55"},
{"苏格拉高","男","45"},
{"苏格拉中","男","65"},
{"李白","男","95"},
{"冗余和","男","40"},
{"和域诗","女","18"}};
public void init(){
JTable jtable = new JTable(data,titles);
jtable.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
TableColumn column_1 = jtable.getColumn(titles[0]);
column_1.setMaxWidth(30);
TableColumn column_3 = jtable.getColumn(titles[2]);
column_3.setMaxWidth(30);
jf.add(new JScrollPane(jtable));
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.pack();
jf.setVisible(true);
}
public static void main(String[] args) {
new Demo24().init();
}
}