GUI编程(3)
Swing
JFrame窗体
package GUI.lesson01.Lesson04;
import javax.swing.*;
import java.awt.*;
public class JFrameDemo {
public static void main(String[] args) {
new MyJFrame().init();
}
}
class MyJFrame extends JFrame{
public void init(){
setVisible(true);
Container contentPane = this.getContentPane();
contentPane.setBackground(Color.RED);
setBounds(100,100,200,200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JLabel jLabel = new JLabel("欢迎来到德莱联盟",SwingConstants.CENTER);
add(jLabel);
}
}
弹窗
package GUI.lesson01.Lesson04;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//主窗口
public class DialogDemo extends JFrame {
public DialogDemo() throws HeadlessException {
setVisible(true);
setBounds(10,10,700,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container contentPane = getContentPane();
contentPane.setLayout(null);
JButton jButton = new JButton("弹出对话框");
jButton.setBounds(30,30,200,50);
contentPane.add(jButton);
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new MyDialog();
}
});
}
public static void main(String[] args) {
new DialogDemo();
}
}
class MyDialog extends JDialog{
public MyDialog() {
setVisible(true);
setBounds(100,100,500,500);
Container container = getContentPane();
container.setLayout(null);
container.add(new Label("欢迎来到德莱联盟"));
}
}
标签
//label
new Label("xxx");
//icon
new Icon();
面板
package GUI.lesson01.lesson05;
import javax.swing.*;
import java.awt.*;
//JPanel
public class TestJPanel extends JFrame {
public TestJPanel() throws HeadlessException {
Container container = getContentPane();
JPanel jPanel = new JPanel(new GridLayout(1,3));
container.setLayout(new GridLayout(2,1,10,10));//后两个参数是间距
jPanel.add(new JButton("1"));
jPanel.add(new JButton("1"));
jPanel.add(new JButton("1"));
container.add(jPanel);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
setBounds(10,10,200,200);
}
public static void main(String[] args) {
new TestJPanel();
}
}
package GUI.lesson01.lesson05;
import javax.swing.*;
import java.awt.*;
public class TestJScroll extends JFrame {
public static void main(String[] args) {
new TestJScroll();
}
public TestJScroll() throws HeadlessException {
Container contentPane = getContentPane();
JTextArea jTextArea = new JTextArea(20, 50);
jTextArea.setText("欢迎来到德莱联盟");
ScrollPane pane = new ScrollPane();
pane.add(jTextArea);
contentPane.add(pane);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
setBounds(10,10,200,200);
}
}
按钮
package GUI.lesson01.lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
//图片按钮
public class TestJButton01 extends JFrame {
public static void main(String[] args) {
new TestJButton01();
}
public TestJButton01() throws HeadlessException {
Container contentPane = getContentPane();
URL resource = TestJButton01.class.getResource("刘念壁纸.jpg");
Icon icon = new ImageIcon(resource);
JButton button = new JButton();
button.setIcon(icon);
button.setToolTipText("图片按钮");
contentPane.add(button);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
setBounds(10,10,200,200);
}
}
package GUI.lesson01.lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
//单选按钮
public class TestJButton01 extends JFrame {
public static void main(String[] args) {
new TestJButton01();
}
public TestJButton01() throws HeadlessException {
Container contentPane = getContentPane();
URL resource = TestJButton01.class.getResource("刘念壁纸.jpg");
Icon icon = new ImageIcon(resource);
JRadioButton button1 = new JRadioButton("button1");
JRadioButton button2 = new JRadioButton("button2");
JRadioButton button3 = new JRadioButton("button3");
//这个组中的选项只能选择一个,从而达到单选框的目的
ButtonGroup group = new ButtonGroup();
group.add(button1);
group.add(button2);
group.add(button3);
contentPane.add(button1,BorderLayout.NORTH);
contentPane.add(button2,BorderLayout.CENTER);
contentPane.add(button3,BorderLayout.SOUTH);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
setBounds(10,10,200,200);
}
}
package GUI.lesson01.lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
//多选框
public class TestJButton01 extends JFrame {
public static void main(String[] args) {
new TestJButton01();
}
public TestJButton01() throws HeadlessException {
Container contentPane = getContentPane();
URL resource = TestJButton01.class.getResource("刘念壁纸.jpg");
Icon icon = new ImageIcon(resource);
JCheckBox box1 = new JCheckBox("box1");
JCheckBox box2 = new JCheckBox("box2");
JCheckBox box3 = new JCheckBox("box3");
contentPane.add(box1,BorderLayout.NORTH);
contentPane.add(box2,BorderLayout.CENTER);
contentPane.add(box3,BorderLayout.SOUTH);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
setBounds(10,10,200,200);
}
}
列表
package GUI.lesson01.lesson06;
import javax.swing.*;
import java.awt.*;
//下拉框
public class TestCombobox extends JFrame {
public static void main(String[] args) {
new TestCombobox();
}
public TestCombobox() throws HeadlessException {
Container container = getContentPane();
JComboBox<String> box = new JComboBox<>();
box.addItem("没有大问题");
box.addItem("有点小问题");
box.addItem("一切正常");
box.addItem(null);
container.add(box);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
setBounds(10,10,500,350);
}
}
package GUI.lesson01.lesson06;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
//表格框
public class TestCombobox2 extends JFrame {
public static void main(String[] args) {
new TestCombobox2();
}
public TestCombobox2() throws HeadlessException {
Container container = getContentPane();
//String[] contents = {"1","2","3","4"};
Vector<String> vector = new Vector<>();
JList<String> list = new JList<>(vector);
vector.add("张三");
vector.add("李四");
vector.add("王五");
container.add(list);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
setBounds(10,10,500,500);
}
}
文本框
package GUI.lesson01.lesson06;
import javax.swing.*;
import java.awt.*;
//文本框
public class TestText extends JFrame {
public static void main(String[] args) {
new TestText();
}
public TestText() throws HeadlessException {
Container container = getContentPane();
JTextField textField1 = new JTextField("hello");
JTextField textField2 = new JTextField("world");
container.add(textField1,BorderLayout.NORTH);
container.add(textField2,BorderLayout.SOUTH);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
setBounds(10,10,500,500);
}
}
package GUI.lesson01.lesson06;
import javax.swing.*;
import java.awt.*;
//密码框
public class TestText extends JFrame {
public static void main(String[] args) {
new TestText();
}
public TestText() throws HeadlessException {
Container container = getContentPane();
JPasswordField passwordField = new JPasswordField();//默认为小点点
//passwordField.setEchoChar('*');
container.add(passwordField);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
setBounds(10,10,500,500);
}
}
package GUI.lesson01.lesson06;
import javax.swing.*;
import java.awt.*;
//文本域
public class TestText extends JFrame {
public static void main(String[] args) {
new TestText();
}
public TestText() throws HeadlessException {
Container container = getContentPane();
TextArea textArea = new TextArea(20,50);
textArea.setText("欢迎来到德莱联盟");
container.add(textArea);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
setBounds(10,10,500,500);
}
}