【发布时间】: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