java Swing GUI 入门-图片和控件可视化

java Swing GUI 入门-图片和控件可视化

觉得有用的话,欢迎一起讨论相互学习~

我的微博我的github我的B站

加入一张图片

注意要保持图片的尺寸,要不然太大了把其他的内容都遮住了

    private void createUIComponents() {
        logoLabel = new JLabel();
        ImageIcon logoIcon = new ImageIcon(new ImageIcon("image/a.png").getImage().getScaledInstance(300, 250, Image.SCALE_SMOOTH));
        logoLabel.setIcon(logoIcon);
    }

控件可视化

  • 通过设置Panel的setVisible属性可以控制一个控件是否可见
        panel1.setVisible(false);
        panel2.setVisible(false);
        panel3.setVisible(false);
        panel4.setVisible(false);
        panel5.setVisible(false);

Jframe 初始大小

  • 在主函数main函数中可以设置窗口的初始大小
        frame.setPreferredSize(new Dimension(800, 800));

程序演示

完整代码

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GolfTourneyManager {
    private JPanel mainPanel;
    private JPanel panel1;
    private JPanel panel2;
    private JPanel panel3;
    private JPanel panel4;
    private JPanel panel5;
    private JTextField textField1;
    private JCheckBox professionalCheckBox;
    private JRadioButton sandpiperRadioButton;
    private JRadioButton mayfairRadioButton;
    private JRadioButton blackHawkRadioButton;
    private JRadioButton theRanchRadioButton;
    private JTextField feeField;
    private JButton REGISTERButton;
    private JSlider slider1;
    private JLabel handiLabel;
    private JLabel logoLabel;
    private JButton registerNowButton;
    private JPanel TitleBarPanel;

    public GolfTourneyManager() {
        professionalCheckBox.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {

                if (professionalCheckBox.isSelected()) {
                    feeField.setText("$120");
                } else {
                    feeField.setText("$100");
                }
            }

        });


        slider1.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                int handicap = slider1.getValue();
                handiLabel.setText("Your Handicap: " + handicap);

            }
        });

        registerNowButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                panel1.setVisible(true);
                panel2.setVisible(true);
                panel3.setVisible(true);
                panel4.setVisible(true);
                panel5.setVisible(true);
            }
        });

        panel1.setVisible(false);
        panel2.setVisible(false);
        panel3.setVisible(false);
        panel4.setVisible(false);
        panel5.setVisible(false);


    }//end constructor

    public static void main(String[] args) {
        JFrame frame = new JFrame("Golf Tourney Manager");
        frame.setContentPane(new GolfTourneyManager().mainPanel);
        frame.setPreferredSize(new Dimension(800, 800));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }


    private void createUIComponents() {
        logoLabel = new JLabel();
        ImageIcon logoIcon = new ImageIcon(new ImageIcon("image/a.png").getImage().getScaledInstance(300, 250, Image.SCALE_SMOOTH));
        logoLabel.setIcon(logoIcon);
    }
}//end class

posted @ 2020-12-21 20:33  WUST许志伟  阅读(743)  评论(0编辑  收藏  举报