【Java】图形界面设计实战练习
作者:互联网
实例一
代码:
package Gui;
import javax.swing.*;
import java.awt.*;
public class MyFrame extends JFrame {
JPanel jP1,jp2,jp3;
JLabel label1,label2;
JButton button1,button2;
JTextField text;
JPasswordField password;
public static void main(String[] args) {
MyFrame myFrame = new MyFrame();
}
public MyFrame(){
init();
setTitle("登录界面");
setBounds(300,300,250,150);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void init(){
setLayout(new GridLayout(3,1));
jP1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
label1 = new JLabel("用户名");
label2 = new JLabel("密 码");
text = new JTextField(10);
password = new JPasswordField(10);
button1 = new JButton("登录");
button2 = new JButton("取消");
jP1.add(label1);
jP1.add(text);
jp2.add(label2);
jp2.add(password);
jp3.add(button1);
jp3.add(button2);
add(jP1);
add(jp2);
add(jp3);
}
}
实例二
package Gui;
import javax.swing.*;
import java.awt.*;
public class MyFrame extends JFrame {
JPanel jp1,jp2,jp3;
JCheckBox checkBox1,checkBox2,checkBox3;
ButtonGroup group;
JRadioButton radioM,radioF;
JLabel label1,label2;
JButton button1,button2;
public static void main(String[] args) {
MyFrame myFrame = new MyFrame();
}
public MyFrame(){
init();
setTitle("用户注册界面");
setBounds(300,300,300,200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void init(){
setLayout(new GridLayout(3,1));
jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
label1 = new JLabel("你喜欢的运动");
label2 = new JLabel("你的性别");
checkBox1 = new JCheckBox("足球");
checkBox2= new JCheckBox("篮球");
checkBox3 = new JCheckBox("网球");
radioM = new JRadioButton("男");
radioF = new JRadioButton("女");
button1 = new JButton("注册用户");
button2 = new JButton("取消用户");
group = new ButtonGroup();
group.add(radioM);
group.add(radioF);
jp1.add(label1);
jp1.add(checkBox1);
jp1.add(checkBox2);
jp1.add(checkBox3);
jp2.add(label2);
jp2.add(radioM);
jp2.add(radioF);
jp3.add(button1);
jp3.add(button2);
add(jp1);
add(jp2);
add(jp3);
}
}
实例三
代码:
package Gui;
import javax.swing.*;
import java.awt.*;
public class MyFrame extends JFrame {
JPanel jp1,jp2,jp3;
JLabel label1,label2;
JComboBox comboBox;
JList list;
JScrollPane jScrollPane;
public static void main(String[] args) {
MyFrame myFrame = new MyFrame();
}
public MyFrame(){
init();
setTitle("下拉框练习");
setBounds(300,300,300,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void init(){
setLayout(new GridLayout(3,1));
jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
label1 = new JLabel("你的籍贯是");
label2 = new JLabel("你喜欢旅游的地区是");
String []str1 = {"北京","上海","天津","重庆","江苏"};
comboBox = new JComboBox(str1);
String []str2 = {"黄山","故宫","长城","九寨沟","天安门","火星"};
list = new JList(str2);
list.setVisibleRowCount(1);
jScrollPane = new JScrollPane(list);
jp1.add(label1);
jp1.add(comboBox);
jp2.add(label2);
jp2.add(jScrollPane);
add(jp1);
add(jp3);
add(jp2);
}
}
实例四
代码:
package Gui;
import javax.swing.*;
import java.awt.*;
public class MyFrame extends JFrame {
JSplitPane jSplitPane;
JList list;
JLabel label;
public static void main(String[] args) {
MyFrame myFrame = new MyFrame();
}
public MyFrame(){
init();
setBounds(300,300,400,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void init(){
String []words = {"boy","gril","bird","box"};
list = new JList(words);
label = new JLabel(new ImageIcon("idea_test/2.jpg"));
jSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,list,label);
jSplitPane.setOneTouchExpandable(true);
add(jSplitPane);
}
}
实例五
代码:
package Gui;
import javax.swing.*;
import java.awt.*;
public class MyFrame extends JFrame {
JSplitPane jSplitPane;
JTextArea area;
JTextField text;
JComboBox comboBox;
JButton button;
JPanel jPanel;
public static void main(String[] args) {
MyFrame myFrame = new MyFrame();
}
public MyFrame(){
init();
setTitle("QQ登录");
setBounds(300,300,300,200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void init(){
String str[] = {"北京","上海","南京","合肥","洛阳","哈尔滨"};
comboBox = new JComboBox(str);
area = new JTextArea();
add(new JScrollPane(area));
text = new JTextField(10);
button = new JButton("发送");
jPanel = new JPanel();
jPanel.add(comboBox);
jPanel.add(text);
jPanel.add(button);
add(jPanel,BorderLayout.SOUTH);
//设置左上角小头像
setIconImage((new ImageIcon("idea_test\\2.jpg")).getImage());
}
}
标签:实战,Java,300,图形界面,add,new,jp2,public,MyFrame 来源: https://blog.csdn.net/weixin_48180029/article/details/113832942