【问题标题】:JComponents arent displayed properly unless the window is minimized and reopened除非窗口被最小化并重新打开,否则组件无法正确显示
【发布时间】:2022-01-19 23:02:57
【问题描述】:

我正在用 Java Swing 编写一个程序,到目前为止我只完成了 GUI,但这就是给我带来问题的原因。我不知道为什么,但是当使用按钮激活我的“任务”方法时,应该显示的 JComponents 没有正确显示。 JPanel“任务”应该是 300x300,但它看起来不像,除非窗口被最小化并重新打开。尺寸不对,一些 JLabel 也被剪掉了。我不知道该怎么做,我尝试重新排列代码,以便主 GUI 框架在之后可见,但这也不起作用。我一直在尝试解决这个问题很长时间。我该怎么办?

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalDate;
import java.util.Date;


public class gui implements ActionListener  {
    
    static JFrame frame;
    Container container;
    JPanel title, open, choicePanel, date, tasks, ttitle, ntes; 
    JLabel titleName, date1, ttitle2, rmndAt, ntes1; 
    Font titleFont = new Font("Lucida Handwriting", Font.PLAIN, 70);              
    Font clickHereFont = new Font("Lucida Handwriting", Font.PLAIN, 18);
    Font stitleFont = new Font("Lucida Handwriting", Font.PLAIN, 50);
    JButton openButton, tasksButton, notesButton, adtButton, tnButton;
    Date datee = new Date(); //date object
     
    {

    frame = new JFrame();
    frame.setSize(1000,700);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); d
    frame.getContentPane().setBackground(new Color(4, 102, 69)); 
    frame.setResizable(false);  
    frame.setLayout(null); 

    date = new JPanel();
    date.setBounds(350,150,300,200); 
    date.setBackground(new Color(14, 21, 7)); 
    date1 = new JLabel("Today is   "+ (LocalDate.now()).toString() + "    ");
    date1.setForeground(new Color(135, 233, 169)); 
    date1.setFont(clickHereFont);
    
    title = new JPanel();
    title.setBounds(170,250,650,120);  
    title.setBackground(new Color(135, 233, 169)); 
    
    titleName = new JLabel("Daily Planner");
    titleName.setForeground(new Color(14, 21, 7));
    titleName.setFont(titleFont); 
    
    
    open = new JPanel();
    open.setBounds(400,420,150,75);
    open.setBackground(new Color(4, 102, 69));
    
    openButton = new JButton("CLICK HERE");
    openButton.setBounds(300,420,200,100);
    openButton.setBackground(new Color(14, 21, 7));
    openButton.setForeground(new Color(135, 233, 169));
    openButton.setFont(clickHereFont);
    openButton.addActionListener(this); 


    date.add(date1); 
    frame.add(date);
    title.add(titleName);
    frame.add(title);
    frame.add(open);
    open.add(openButton);
    frame.setVisible(true);
    }


    public static void main(String[] args) {
        
        new gui();
    }
        
        public gui()  {
            
        }
        
        public void choice() {
            
            title.setVisible(false);
            open.setVisible(false);
            date.setVisible(false);
            
            choicePanel = new JPanel();
            choicePanel.setBounds(160,100,650,500);
            choicePanel.setBackground(new Color(135, 233, 169));
            choicePanel.setLayout(new GridLayout(1,2)); 
            
            tasksButton = new JButton("TASKS");
            tasksButton.setBackground(new Color (6, 122, 82));
            tasksButton.setFont(titleFont); //reused the "titleFont" font 
            tasksButton.setForeground(Color.white);
            tasksButton.addActionListener(this);
            
            notesButton = new JButton("NOTES");
            notesButton.setBackground(new Color(63, 194, 131));
            notesButton.setFont(titleFont);  
            notesButton.setForeground(Color.white);
            notesButton.addActionListener(this);
            
            choicePanel.add(tasksButton);     
            choicePanel.add(notesButton);
            frame.add(choicePanel);
        }
        
        
        
        public void tasks() {

            choicePanel.setVisible(false);
        
            
            tasks = new JPanel();
            tasks.setBounds(30,150,300,300);
            tasks.setBackground(new Color(6, 122, 76));
        
    
            ttitle = new JPanel(); 
            ttitle.setBackground(new Color(4, 102, 69));
            ttitle.setBounds(20,50,500,100);
            
            ttitle2 = new JLabel("TASKS");
            ttitle2.setBounds(50,50,150,45);
            ttitle2.setBackground(new Color(4, 102, 69));
            ttitle2.setForeground(Color.white);
            ttitle2.setFont(stitleFont);
            
            adtButton = new JButton("+");
            adtButton.setBounds(550,50,80,80);
            adtButton.setBackground(new Color(4, 102, 69));
            adtButton.setForeground(Color.white);
            adtButton.setFont(stitleFont);

            
            
            rmndAt = new JLabel("REMIND AT");
            rmndAt.setBounds(750,50,200,95);
            rmndAt.setBackground(new Color(4, 102, 69));
            rmndAt.setForeground(Color.white);
            rmndAt.setFont(clickHereFont);
    
            frame.add(tasks);
            ttitle.add(ttitle2);
            frame.add(ttitle);
            frame.add(adtButton);
            frame.add(rmndAt);
            
        }
        
        
        public void ntess() {
            choicePanel.setVisible(false);
            title.setVisible(false);
            
            ntes = new JPanel();
            ntes.setBackground(new Color(6, 122, 76));
            ntes.setBounds(30,150,200,200);
            
            frame.add(ntes);
            
        }

        @Override
        public void actionPerformed(ActionEvent e) {  
            if (e.getSource() == openButton) {     
                choice(); 
            }
            else if (e.getSource()==notesButton) {
                ntess();
            }
            else if (e.getSource() == tasksButton) {
                tasks();
            }
            
            
        }

    
    
            
          
            
       
    

    } 

【问题讨论】:

  • 1) 不要使用空布局。不要使用 setBounds()。 Swing 旨在与布局管理器一起使用。 2)在框架可见之前向框架添加组件。
  • “你提供的截图不是我的代码” 我的错。你是对的。这一定是IDE的问题。

标签: java swing jpanel layout-manager jcomponent


【解决方案1】:

我只是想要一个答案,并想让这个程序运行起来

要非常小心你想要什么......

以下使用以下概念...

该示例也不完整,您需要花时间理解上述概念才能填写缺失的部分

我还认为您会想在某个时候查看How to Use Tables

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new NavigationPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public static class Support {
        public static final Font TITLE_FONT = new Font("Lucida Handwriting", Font.PLAIN, 70);
        public static final Font NORMAL_FONT = new Font("Lucida Handwriting", Font.PLAIN, 18);
        public static final Font SMALL_FONT = new Font("Lucida Handwriting", Font.PLAIN, 18);
    }

    public class NavigationPane extends JPanel {

        protected enum View {
            MAIN, CHOICE, NOTES, TASKS;
        }

        private CardLayout cardLayout;

        public NavigationPane() {
            cardLayout = new CardLayout();
            setLayout(cardLayout);

            add(new MainPane(new MainPane.Observer() {
                @Override
                public void navigateToChoices(MainPane source) {
                    navigateTo(View.CHOICE);
                }
            }), View.MAIN);

            add(new ChoicePane(new ChoicePane.Observer() {
                @Override
                public void navigateToTasks(ChoicePane source) {
                    navigateTo(View.TASKS);
                }

                @Override
                public void navigateToNotes(ChoicePane source) {
                    navigateTo(View.NOTES);
                }
            }), View.CHOICE);

            add(new TasksPane(), View.TASKS);
            add(new NotesPane(), View.NOTES);

            navigateTo(View.MAIN);
        }

        protected void navigateTo(View view) {
            cardLayout.show(this, view.name());
        }

        // Because I'm lazy
        protected void add(Component comp, View view) {
            super.add(comp, view.name());
        }

    }

    public class MainPane extends JPanel {

        public interface Observer {
            public void navigateToChoices(MainPane source);
        }

        private Observer observer;

        public MainPane(Observer observer) {
            this.observer = observer;
            setBorder(new EmptyBorder(32, 32, 32, 32));
            setBackground(new Color(4, 102, 69));
            setLayout(new GridBagLayout());

            JLabel todayLabel = new JLabel("Today is " + DateTimeFormatter.ISO_LOCAL_DATE.format(LocalDate.now()));
            todayLabel.setFont(Support.NORMAL_FONT);
            todayLabel.setForeground(new Color(135, 233, 169));
            todayLabel.setBackground(new Color(14, 21, 7));
            todayLabel.setOpaque(true);
            todayLabel.setBorder(new EmptyBorder(16, 16, 16, 16));

            JLabel titleLabel = new JLabel("Daily Planner");
            titleLabel.setFont(Support.TITLE_FONT);
            titleLabel.setBackground(new Color(135, 233, 169));
            titleLabel.setOpaque(true);
            titleLabel.setBorder(new EmptyBorder(32, 32, 32, 32));

            JButton clickHereButton = new JButton("Click Here");
            clickHereButton.setFont(Support.SMALL_FONT);

            clickHereButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    observer.navigateToChoices(MainPane.this);
                }
            });

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridy = 0;
            gbc.gridwidth = gbc.REMAINDER;
            gbc.insets = new Insets(8, 8, 32, 8);            
            add(todayLabel, gbc);

            gbc.gridy++;
            gbc.insets = new Insets(0, 8, 32, 8);
            add(titleLabel, gbc);

            gbc.gridy++;
            gbc.insets = new Insets(0, 8, 8, 8);
            add(clickHereButton, gbc);
        }

    }

    public class ChoicePane extends JPanel {

        public interface Observer {
            public void navigateToTasks(ChoicePane source);
            public void navigateToNotes(ChoicePane source);
        }

        private Observer observer;

        public ChoicePane(Observer observer) {
            this.observer = observer;
            setBorder(new EmptyBorder(32, 32, 32, 32));
            setBackground(new Color(4, 102, 69));
            setLayout(new GridLayout(1, 2, 8, 8));

            JButton tasksButton = new JButton("TASKS");
            tasksButton.setOpaque(true);
            tasksButton.setBorderPainted(false);
            tasksButton.setBackground(new Color(63, 194, 131));
            tasksButton.setFont(Support.TITLE_FONT); //reused the "titleFont" font 
            tasksButton.setForeground(Color.white);
            tasksButton.setFocusPainted(false);
            tasksButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    observer.navigateToTasks(ChoicePane.this);
                }
            });

            JButton notesButton = new JButton("NOTES");
            notesButton.setOpaque(true);
            notesButton.setBorderPainted(false);
            notesButton.setBackground(new Color(63, 194, 131));
            notesButton.setFont(Support.TITLE_FONT);
            notesButton.setForeground(Color.white);
            notesButton.setFocusPainted(false);
            notesButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    observer.navigateToNotes(ChoicePane.this);
                }
            });

            add(tasksButton);
            add(notesButton);
        }

    }

    public class TasksPane extends JPanel {

        public TasksPane() {
            setBorder(new EmptyBorder(32, 32, 32, 32));
            setBackground(new Color(4, 102, 69));
            setLayout(new BorderLayout(8, 8));

            JPanel titlePane = new JPanel(new GridBagLayout());
            titlePane.setOpaque(false);

            JLabel titleLabel = new JLabel("Tasks");
            titleLabel.setBackground(new Color(4, 102, 69));
            titleLabel.setForeground(Color.WHITE);
            titleLabel.setFont(Support.SMALL_FONT);

            JButton addButton = new JButton("+");
            addButton.setBorderPainted(false);
            addButton.setFocusPainted(false);
            addButton.setBackground(new Color(63, 194, 131));
            addButton.setForeground(Color.white);
            addButton.setFont(Support.SMALL_FONT);
            addButton.setOpaque(true);

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = 1;
            gbc.anchor = gbc.LINE_START;
            titlePane.add(titleLabel, gbc);
            gbc.weightx = 0;
            gbc.gridx++;
            titlePane.add(addButton, gbc);

            add(titlePane, BorderLayout.NORTH);

            // Personally, I think you need a JTable
            JPanel fillerPane = new JPanel();
            fillerPane.setBackground(new Color(6, 122, 76));
            add(fillerPane);
        }

    }

    public class NotesPane extends JPanel {

        public NotesPane() {
            setBorder(new EmptyBorder(32, 32, 32, 32));
            setBackground(new Color(4, 102, 69));
            setLayout(new BorderLayout(8, 8));

            JPanel titlePane = new JPanel(new GridBagLayout());
            titlePane.setOpaque(false);

            JLabel titleLabel = new JLabel("Notes");
            titleLabel.setBackground(new Color(4, 102, 69));
            titleLabel.setForeground(Color.WHITE);
            titleLabel.setFont(Support.SMALL_FONT);

            JButton addButton = new JButton("+");
            addButton.setBorderPainted(false);
            addButton.setFocusPainted(false);
            addButton.setBackground(new Color(63, 194, 131));
            addButton.setForeground(Color.white);
            addButton.setFont(Support.SMALL_FONT);
            addButton.setOpaque(true);

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = 1;
            gbc.anchor = gbc.LINE_START;
            titlePane.add(titleLabel, gbc);
            gbc.weightx = 0;
            gbc.gridx++;
            titlePane.add(addButton, gbc);

            add(titlePane, BorderLayout.NORTH);

            // Personally, I think you need a JTable
            JPanel fillerPane = new JPanel();
            fillerPane.setBackground(new Color(6, 122, 76));
            add(fillerPane);
        }

    }
}

对比你的输出(左)和我的输出(右)

除了免费获得可重用性之外,假设您的客户回来说,“我想更改字体”,或者更糟的是,“我只想更改这几个元素的字体”。

您认为哪种方法会更好地应对?

或者,你遇到了像我这样的用户,他的盲人像蝙蝠一样,并且系统的字体可访问性得到了提升?

【讨论】:

  • 非常感谢!我将查看您发布的链接以及代码!欣赏它
猜你喜欢
  • 2014-12-03
  • 1970-01-01
  • 2013-07-17
  • 2021-04-15
  • 1970-01-01
  • 2016-07-30
  • 1970-01-01
  • 2012-04-12
  • 2020-10-28
相关资源
最近更新 更多