zoukankan      html  css  js  c++  java
  • 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
    
    
  • 相关阅读:
    Linux_KVM虚拟机
    Linux_KVM虚拟机
    Python_编程特色
    Python_编程特色
    Linux_进程管理&计划任务
    Linux_进程管理&计划任务
    Linux_系统破坏性修复实验
    Linux_系统破坏性修复实验
    Linux_Shell基础
    Confluence 6 的高级 Crowd 设置
  • 原文地址:https://www.cnblogs.com/cloud-ken/p/14169828.html
Copyright © 2011-2022 走看看