gpt4 book ai didi

java - JLabel 最初不显示

转载 作者:行者123 更新时间:2023-12-02 10:36:56 24 4
gpt4 key购买 nike

我做了一个简单的类(class)来练习布局。大部分工作正常,但我的 JLabel 直到我单击按钮后才出现。我在早期版本中拥有与 JTextFieldJTextArea 相同的信息,但确实更喜欢 JLabel 的外观,但即使在其他迭代中也是如此,只有当我单击或尝试从窗口中选择文本时,它才会出现。我尝试在生成 text 变量后将其设置为可见,将其添加到 under 面板后,并将整个 under 面板设置为除了为整个对象调用的 setVisible(true) 之外,还可以使用可见的方法,但这些都不起作用。

这是我的代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class LayoutPractice extends JFrame implements ActionListener {

//Set up variables
private JPanel graphic;
private JPanel under;
private JButton button;
private JLabel text;
private int clicks;

/**
* Constructor, sets up GUI
*
*/
public LayoutPractice(){
//Default JFrame setup
super("Layout Practice");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set up the graphic panel
this.graphic = new JPanel();
graphic.setPreferredSize(new Dimension(500, 500));

//Set up the components that go under the graphic
this.clicks = 0;
this.button = new JButton("Click for dialog");
this.text = new JLabel("No Clicks");

//Set up the under panel, add in the button and label
this.under = new JPanel();
under.setLayout(new FlowLayout());
under.add(button);
under.add(text);

//Tell it to react to the button being pressed
button.addActionListener(this);

//Set the panels onto the JFrame
getContentPane().add(graphic, BorderLayout.CENTER);
getContentPane().add(under, BorderLayout.PAGE_END);

//Pack and set the JFrame to visible
pack();
setVisible(true);
}

/**
* Paints the image displayed on graphic
*
* @param A Graphics object to be worked on
*
*/
public void paint(Graphics g) {
//Assigns which panel to paint
graphic.paint(g);
//Set a color to pains
g.setColor(Color.BLUE);
//Use variables for a pattern
int x = 0;
int y = 0;
//Loop for a pattern
while (x <= 230) {
//Generates a filled rectangle of the correct size
g.fillRect(x, y, (500-(2 * x)), (500-(2 * y)));
//Alternates color
if (g.getColor() == Color.BLUE) {
g.setColor(Color.ORANGE.darker());
}
else {
g.setColor(Color.BLUE);
}
//Increase variables to reduce rectangle size
x += 20;
y += 20;
}
}

/**
* Tells the GUI what to do when the button is pressed
*
* @param An ActionEvent, specifically the buton being pressed
*/
public void actionPerformed(ActionEvent event) {
//Increase the clicks variable
clicks++;
//Change/update the JLabel
text.setText("Clicks: " + clicks);
//Open a dialog using available tools
JOptionPane.showMessageDialog(new JFrame(),
("Clicks: " + clicks),
"Click Count",
JOptionPane.INFORMATION_MESSAGE);
}

/**
* Very simple main, makes a new LayoutPractice
*
* @param args
*/
public static void main(String[] args) {
new LayoutPractice();
}


}

最佳答案

快速解决方法是在重写的 paint 方法的开头调用 super.paint(g) ,以便框架确保其清除/清理/布局正确。

最佳修复考虑以下因素:

当重写一个方法时,添加@Override注释,这样如果你错误地重写了,你的IDE就会警告你。

重写绘画方法时,调用其 super 实现以确保父组件正确清理内容。

对于自定义绘画,最好使用 JComponent(通常是 JPanel)。

对于自定义绘画,请重写paintComponent(Graphics)(并调用super.paintComponent),不要使用paint(Graphics)

您不需要扩展 JFrame,只需创建一个 JFrame 并使用它即可。

在下面的示例中,已添加自定义 JPanel 类用于自定义绘制,并且应用程序不再扩展 JFrame :

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class LayoutPractice implements ActionListener {
//Set up variables
private final JPanel graphic;
private final JPanel under;
private final JButton button;
private final JLabel text;
private int clicks;

/**
* Constructor, sets up GUI
*
*/
public LayoutPractice() {
//Default JFrame setup

JFrame frame = new JFrame("Layout Practice");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set up the graphic panel
graphic = new GraphicPanel();
graphic.setPreferredSize(new Dimension(500, 500));

//Set up the components that go under the graphic
clicks = 0;
button = new JButton("Click for dialog");
text = new JLabel("No Clicks");

//Set up the under panel, add in the button and label
under = new JPanel();
under.setLayout(new FlowLayout());
under.add(button);
under.add(text);

//Tell it to react to the button being pressed
button.addActionListener(this);

JPanel mainPanel = new JPanel(new BorderLayout());

//Set the panels onto the JFrame
mainPanel.add(graphic, BorderLayout.CENTER);
mainPanel.add(under, BorderLayout.PAGE_END);

frame.setContentPane(mainPanel);

//Pack and set the JFrame to visible
frame.pack();
frame.setVisible(true);
}

/**
* Tells the GUI what to do when the button is pressed
*
* @param An ActionEvent, specifically the buton being pressed
*/
public void actionPerformed(final ActionEvent event) {
//Increase the clicks variable
clicks++;
//Change/update the JLabel
text.setText("Clicks: " + clicks);
//Open a dialog using available tools
JOptionPane.showMessageDialog(new JFrame(),
("Clicks: " + clicks),
"Click Count",
JOptionPane.INFORMATION_MESSAGE);
}

/**
* Very simple main, makes a new LayoutPractice
*
* @param args
*/
public static void main(final String[] args) {
new LayoutPractice();
}

private class GraphicPanel extends JPanel {

@Override
public void paintComponent(final Graphics g) {

super.paintComponent(g);

//Set a color to pains
g.setColor(Color.BLUE);
//Use variables for a pattern
int x = 0;
int y = 0;
//Loop for a pattern
while (x <= 230) {
//Generates a filled rectangle of the correct size
g.fillRect(x, y, (500 - (2 * x)), (500 - (2 * y)));
//Alternates color
if (g.getColor() == Color.BLUE) {
g.setColor(Color.ORANGE.darker());
} else {
g.setColor(Color.BLUE);
}
//Increase variables to reduce rectangle size
x += 20;
y += 20;
}
}
}



}

关于java - JLabel 最初不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53221304/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com