Swing
1. JFrame
package com.langtao.base.demo12;
public class Application {
public static void main(String[] args) {
new JframeDemo().init();
}
}
package com.langtao.base.demo12;
import javax.swing.;
import java.awt.;
import java.awt.event.*;
public class JframeDemo extends JFrame {
public void init(){
setBounds(100,100,200,400);
Container container = this.getContentPane();//设置(窗口)容器的背景颜色。
container.setBackground(Color.cyan);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//关闭窗口
JLabel label = new JLabel("Welcome!");
add(label);
//label.setHorizontalAlignment(SwingConstants.CENTER);//设置label居中
setVisible(true);
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
System.out.println(e.getKeyCode());
}
});
label.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
super.mousePressed(e);
JLabel jLabel = (JLabel)e.getSource();
if(jLabel.getText() == "Welcome!"){
label.setText("Goodbye.");
}else{
label.setText("Welcome!");
}
}
});
}
}
2. JDialog
package com.langtao.base.demo13;
public class Application {
public static void main(String[] args) {
new JDialogTest().init();
}
}
package com.langtao.base.demo13;
import javax.swing.;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class JDialogTest extends JFrame {
public void init(){
this.setVisible(true);
this.setBounds(100,100,300,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = this.getContentPane();
container.setBackground(Color.GRAY);
JButton button = new JButton("Information.");
container.setLayout(null);
container.add(button);
container.setVisible(true);
button.setBounds(10,10,200,30);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new MyDialog();
}
});
}
class MyDialog extends JDialog{
public MyDialog() {
setVisible(true);
setBounds(200,100,400,500);
Container container = this.getContentPane();
container.setBackground(Color.ORANGE);
container.setLayout(null);
JLabel jLabel = new JLabel("Attention Please!");
jLabel.setBounds(10,10,100,100);
container.add(jLabel);
container.setVisible(true);
}
}
}
3. Label + Icon
package com.langtao.base.demo13;
public class Application {
public static void main(String[] args) {
new JDialogTest().init();
}
}
package com.langtao.base.demo14;
import javax.swing.;
import java.awt.;
import java.awt.image.ImageObserver;
import java.text.AttributedCharacterIterator;
public class JLabelDemo extends JFrame {
private MyIcon myIcon = new MyIcon();
private JLabel label = new JLabel("hello", myIcon, SwingConstants.CENTER);
public void init(){
setBounds(100,100,300,500);
Container container = getContentPane();
container.setBackground(Color.ORANGE);
container.add(label);
label.setHorizontalAlignment(SwingConstants.CENTER);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
class MyIcon implements Icon{
private final int iconWidth = 10;
private final int iconHeight = 10;
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillRect(x,y, iconWidth,iconHeight);
g.setColor(Color.GREEN);
}
@Override
public int getIconWidth() {
return this.iconWidth;
}
@Override
public int getIconHeight() {
return this.iconHeight;
}
}
}
4. ImageIcon
URL url = JLabelDemo.class.getResource("20201218010201.jpg");
private ImageIcon imageIcon = new ImageIcon(url);
5. JScrollPane
package com.langtao.base.demo15;
public class Application {
public static void main(String[] args) {
new JScrollPanelTest().init();
}
}
package com.langtao.base.demo15;
import javax.swing.;
import java.awt.;
public class JScrollPanelTest extends JFrame {
public void init(){
setVisible(true);
Container container = this.getContentPane();
setSize(500,500);
container.setBackground(Color.PINK);
container.setLayout(new GridLayout(2,1));
JTextArea jTextArea = new JTextArea();
//jTextArea.setSize(200,2);
JScrollPane jScrollPane = new JScrollPane(jTextArea);
container.add(jScrollPane);
container.setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
6. JButton, JRadioButtion, JCheckBox
package com.langtao.base.demo16;
public class Application {
public static void main(String[] args) {
new JButtonDemo().init();
}
}
package com.langtao.base.demo16;
import javax.swing.;
import java.awt.;
import java.net.URL;
public class JButtonDemo {
private JFrame jFrame = new JFrame("JFrame");
URL url = JButtonDemo.class.getResource("20201218010201.jpg");
private ImageIcon imageIcon = new ImageIcon(url);
private JButton jButton = new JButton(imageIcon);
private JRadioButton jRadioButton1 = new JRadioButton("jRadioButton1");
private JRadioButton jRadioButton2 = new JRadioButton("jRadioButton2");
private JRadioButton jRadioButton3 = new JRadioButton("jRadioButton3");
private ButtonGroup buttonGroup = new ButtonGroup();
private JCheckBox jCheckBox1 = new JCheckBox("jCheckBox1");
private JCheckBox jCheckBox2 = new JCheckBox("jCheckBox2");
private JCheckBox jCheckBox3 = new JCheckBox("jCheckBox3");
private JPanel jPanel1 = new JPanel();
private JPanel jPanel2 = new JPanel();
private JPanel jPanel3 = new JPanel();
public void init(){
Container container = jFrame.getContentPane();
container.setLayout(new GridLayout(3,1));
jPanel1.add(jButton);
container.add(jPanel1);
buttonGroup.add(jRadioButton1);
buttonGroup.add(jRadioButton2);
buttonGroup.add(jRadioButton3);
jPanel2.setLayout(new BorderLayout());
jPanel2.add(jRadioButton1,BorderLayout.NORTH);
jPanel2.add(jRadioButton2,BorderLayout.CENTER);
jPanel2.add(jRadioButton3,BorderLayout.SOUTH);
jPanel2.setBackground(Color.GREEN);
container.add(jPanel2);
jPanel3.setLayout(new BorderLayout());
jPanel3.add(jCheckBox1,BorderLayout.NORTH);
jPanel3.add(jCheckBox2,BorderLayout.CENTER);
jPanel3.add(jCheckBox3,BorderLayout.SOUTH);
jPanel3.setBackground(Color.PINK);
container.add(jPanel3);
jFrame.setVisible(true);
jFrame.setSize(300,500);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
7. JComboBox, JList
package com.langtao.base.demo17;
public class Application {
public static void main(String[] args) {
JComboBoxtest jComboBoxtest = new JComboBoxtest();
JListTest jListTest = new JListTest();
}
}
package com.langtao.base.demo17;
import javax.swing.;
import java.awt.;
public class JComboBoxtest extends JFrame {
private JComboBox jComboBox = new JComboBox();
public JComboBoxtest() {
Container container = getContentPane();
jComboBox.addItem("热映中");
jComboBox.addItem("敬请期待");
jComboBox.addItem("已下架");
container.add(jComboBox);
this.setSize(200,300);
this.setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
package com.langtao.base.demo17;
import javax.swing.;
import java.awt.;
public class JListTest extends JFrame {
private JList jList = new JList();
public JListTest() {
Container container = this.getContentPane();
String[] str = {"Name:", "Grade:", "Gender"};
jList = new JList(str);
container.add(jList);
this.setSize(300,400);
this.setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}

浙公网安备 33010602011771号