Swing

Posted arroa

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Swing相关的知识,希望对你有一定的参考价值。

JFrame jf = new JFrame("JFrame窗口");
jf.setVisible();可见性
jf.setBounds();大小位置
jf.setBackground(Color.cyan);
JLabel label = new JLabel("设置文字");
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);关闭事件

弹窗
public class DialogDemo extends JFrame {
public DialogDemo(){
this.setVisible(true);
this.setSize(700,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = this.getContentPane();
container.setLayout(null);
JButton button = new JButton("button");
button.setBounds(30,30,200,50);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new MyDialogDemo();
}
});
container.add(button);
}
public static void main(String[] args) {
new DialogDemo();
}
}
class MyDialogDemo extends JDialog{
public MyDialogDemo() {
this.setVisible(true);
this.setBounds(200,200,200,200);
Container container = this.getContentPane();
container.setLayout(null);
container.add(new Label("设置文字"));
}
}

标签
public class IconDemo extends JFrame implements Icon {
private int width;
private int height;
public IconDemo(){}
public IconDemo(int width,int height){
this.width = width;
this.height = height;
}
public void init(){
IconDemo iconDemo = new IconDemo(15, 15);
JLabel label = new JLabel("icontest", iconDemo, SwingConstants.CENTER);
Container container = getContentPane();
container.add(label);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new IconDemo().init();
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x,y,width,height);
}
@Override
public int getIconWidth() {
return this.width;
}
@Override
public int getIconHeight() {
return this.height;
}
}

图片Icon
public class ImageIconDemo extends JFrame {
public ImageIconDemo(){
JLabel label = new JLabel("ImageIcon");
URL url = ImageIconDemo.class.getResource("C:\Users\张孛\Pictures\1111.jpg");
ImageIcon imageIcon = new ImageIcon(url);
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER);
Container container = getContentPane();
container.add(label);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(100,100,200,200);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}

面板

按钮
public class JButtonDemo extends JFrame {
public JButtonDemo01() {
Container container = this.getContentPane();
URL resource = JButtonDemo01.class.getResource("C:\Users\张孛\Pictures\1111.jpg");
Icon icon = new ImageIcon(resource);
JButton button = new JButton();
button.setIcon(icon);
button.setToolTipText("图片按钮");
container.add(button);
this.setVisible(true);
this.setSize(500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo01();
}
}

以上是关于Swing的主要内容,如果未能解决你的问题,请参考以下文章

老Java程序员谈谈swing要不要学