JLabel 文本未出现
JLabel text not appearing
我是 Java 的新手。单击虚拟按钮后,我试图使文本显示在 JLabel
上。但是,我似乎无法找到解决方案。当我使用 if 语句时,它不起作用。如何在按下按钮后显示文本?
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.Graphics;
public class autos extends JLabel implements ActionListener
{
private static final long serialVersionUID = 1L;
int now=0;
public autos(){
JLabel l=new JLabel("");
JFrame f=new JFrame("the title");
JPanel p=new JPanel();
JButton b=new JButton("click");
f.setBounds(400,500,400,500);
f.setVisible(true);
p.add(b);
f.add(p);
b.addActionListener(this);
p.setVisible(true);
p.add(l);
f.add(l);
if(now==1)
{
l.setText("hello");
l.setOpaque(true);
}
p.setBounds(200,200,200,200);
l.setBounds(100,100,100,100);
l.setOpaque(true);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawRect(200,300,89,90);
g.drawString("buv",80,80);
repaint();
}
public static void main(String[] args)
{
new autos();
}
@Override
public void actionPerformed(ActionEvent e) {
now=1;
System.out.println("worked");
System.out.println(now);
}
}
您正在构造函数代码中设置标签,该代码在将 now
变量设置为 1 的事件处理程序之前执行。
你能做的就是移动这段代码:
l.setText("hello");
l.setOpaque(true);
到这里:
@Override
public void actionPerformed(ActionEvent e) {
now=1;
System.out.println("worked");
System.out.println(now);
l.setText("hello");
l.setOpaque(true);
}
这是一个在单击按钮时更新文本的最小工作示例。有关各种更改,请参阅代码中的注释。
import java.awt.event.*;
import javax.swing.*;
/* There is no need to extend label here. */
// public class autos extends JLabel implements ActionListener
public class autos implements ActionListener {
private static final long serialVersionUID = 1L;
int now = 0;
// this is now a class attribute, accessible to any method of this class.
JLabel l;
public autos() {
// this no longer declares a local variable, but instead
// creates an instance of the class attribute.
l = new JLabel("");
JFrame f = new JFrame("the title");
JPanel p = new JPanel();
JButton b = new JButton("click");
f.setBounds(400, 500, 400, 500); // this needs fixing!
f.setVisible(true);
p.add(b);
f.add(p);
b.addActionListener(this);
p.setVisible(true);
p.add(l);
f.add(l);
p.setBounds(200, 200, 200, 200); // this needs fixing!
l.setBounds(100, 100, 100, 100); // this needs fixing!
l.setOpaque(true);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
}
/* I cannot tell what this was trying to achieve, but whatever it was,
this was the wrong way to go about it. Never call repaint() from within
the paintComponent method as this creates an infinite loop! */
/*
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawRect(200, 300, 89, 90);
g.drawString("buv", 80, 80);
repaint();
}
*/
public static void main(String[] args) {
// Swing GUIs should be created and updated on the EDT
new autos();
}
@Override
public void actionPerformed(ActionEvent e) {
/* This logic is questionable, but it denpends on what you are trying
to achieve here, something I'm not clear on. */
now = 1;
if (now == 1) {
l.setText("hello");
l.setOpaque(true);
}
System.out.println("worked");
System.out.println(now);
}
}
编辑
You added two comments at the setBounds
part saying this needs fixing!
. There I tried to resize the JPanel
and JLabel
but it's obvious it doesn't work.
How should I proceed here?
这里有一些 'copy/paste comments' 我经常使用的:
- Java GUI 必须在不同的 OS'、屏幕尺寸、屏幕分辨率等上工作,在不同的语言环境中使用不同的 PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或 combinations of them along with layout padding and borders for white space。
- 参见 Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?(是。)
- 提供最小尺寸的 GUI 预期 布局的 ASCII 艺术或简单绘图,如果可调整大小,则提供更大的宽度和高度。
现在,扩展针对此用例的评论:
- 这是我为试图制作
null
布局的人提供的建议,但它也适用于此。
- 这是相关的,因为有一个组件(自定义涂漆组件,如果存在的话)需要
@Override
getPreferredSize(..)
方法 return 一种尺寸作为建议布局管理器或用于打包顶层容器。
- 第三条评论是因为在不知道所需的最终效果的情况下很难建议如何编写此 GUI。
当然,我应该指出:每个 SO 线程都应该是一个单一的、特定的问题。我真的应该告诉你就其他问题开始一个新问题,但让它在这次编辑中滑动。
我是 Java 的新手。单击虚拟按钮后,我试图使文本显示在 JLabel
上。但是,我似乎无法找到解决方案。当我使用 if 语句时,它不起作用。如何在按下按钮后显示文本?
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.Graphics;
public class autos extends JLabel implements ActionListener
{
private static final long serialVersionUID = 1L;
int now=0;
public autos(){
JLabel l=new JLabel("");
JFrame f=new JFrame("the title");
JPanel p=new JPanel();
JButton b=new JButton("click");
f.setBounds(400,500,400,500);
f.setVisible(true);
p.add(b);
f.add(p);
b.addActionListener(this);
p.setVisible(true);
p.add(l);
f.add(l);
if(now==1)
{
l.setText("hello");
l.setOpaque(true);
}
p.setBounds(200,200,200,200);
l.setBounds(100,100,100,100);
l.setOpaque(true);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawRect(200,300,89,90);
g.drawString("buv",80,80);
repaint();
}
public static void main(String[] args)
{
new autos();
}
@Override
public void actionPerformed(ActionEvent e) {
now=1;
System.out.println("worked");
System.out.println(now);
}
}
您正在构造函数代码中设置标签,该代码在将 now
变量设置为 1 的事件处理程序之前执行。
你能做的就是移动这段代码:
l.setText("hello");
l.setOpaque(true);
到这里:
@Override
public void actionPerformed(ActionEvent e) {
now=1;
System.out.println("worked");
System.out.println(now);
l.setText("hello");
l.setOpaque(true);
}
这是一个在单击按钮时更新文本的最小工作示例。有关各种更改,请参阅代码中的注释。
import java.awt.event.*;
import javax.swing.*;
/* There is no need to extend label here. */
// public class autos extends JLabel implements ActionListener
public class autos implements ActionListener {
private static final long serialVersionUID = 1L;
int now = 0;
// this is now a class attribute, accessible to any method of this class.
JLabel l;
public autos() {
// this no longer declares a local variable, but instead
// creates an instance of the class attribute.
l = new JLabel("");
JFrame f = new JFrame("the title");
JPanel p = new JPanel();
JButton b = new JButton("click");
f.setBounds(400, 500, 400, 500); // this needs fixing!
f.setVisible(true);
p.add(b);
f.add(p);
b.addActionListener(this);
p.setVisible(true);
p.add(l);
f.add(l);
p.setBounds(200, 200, 200, 200); // this needs fixing!
l.setBounds(100, 100, 100, 100); // this needs fixing!
l.setOpaque(true);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
}
/* I cannot tell what this was trying to achieve, but whatever it was,
this was the wrong way to go about it. Never call repaint() from within
the paintComponent method as this creates an infinite loop! */
/*
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawRect(200, 300, 89, 90);
g.drawString("buv", 80, 80);
repaint();
}
*/
public static void main(String[] args) {
// Swing GUIs should be created and updated on the EDT
new autos();
}
@Override
public void actionPerformed(ActionEvent e) {
/* This logic is questionable, but it denpends on what you are trying
to achieve here, something I'm not clear on. */
now = 1;
if (now == 1) {
l.setText("hello");
l.setOpaque(true);
}
System.out.println("worked");
System.out.println(now);
}
}
编辑
You added two comments at the
setBounds
part sayingthis needs fixing!
. There I tried to resize theJPanel
andJLabel
but it's obvious it doesn't work.
How should I proceed here?
这里有一些 'copy/paste comments' 我经常使用的:
- Java GUI 必须在不同的 OS'、屏幕尺寸、屏幕分辨率等上工作,在不同的语言环境中使用不同的 PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或 combinations of them along with layout padding and borders for white space。
- 参见 Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?(是。)
- 提供最小尺寸的 GUI 预期 布局的 ASCII 艺术或简单绘图,如果可调整大小,则提供更大的宽度和高度。
现在,扩展针对此用例的评论:
- 这是我为试图制作
null
布局的人提供的建议,但它也适用于此。 - 这是相关的,因为有一个组件(自定义涂漆组件,如果存在的话)需要
@Override
getPreferredSize(..)
方法 return 一种尺寸作为建议布局管理器或用于打包顶层容器。 - 第三条评论是因为在不知道所需的最终效果的情况下很难建议如何编写此 GUI。
当然,我应该指出:每个 SO 线程都应该是一个单一的、特定的问题。我真的应该告诉你就其他问题开始一个新问题,但让它在这次编辑中滑动。