求助:写了一个代码,想让label中的话都显示出来
import java.awt.Button;import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ButtonEvent1 extends Frame{
public static void main(String[] args) {
new ButtonEvent();
}
Button button=new Button("测试按钮");
Panel panel=new Panel();
Label label=new Label("");
public ButtonEvent1(){
super("按钮的事件处理");
this.setLayout(new FlowLayout());
panel.add(button);
panel.add(label);
this.add(panel);
button.addActionListener(new MyEvent());//添加监听
setSize(250,120);
this.setVisible(true);
}
//内部类,处理单击事件
class MyEvent implements ActionListener{
public void actionPerformed(ActionEvent e) {
label.setText("你单击了按钮");
// System.out.println();
}
}
}
要求:当单击按钮的时候在标签上面显示你单击了按钮
[ 本帖最后由 散页 于 2012-11-28 13:44 编辑 ]
----------------解决方案--------------------------------------------------------
除了JLabel label=new JLabel("你单击了按钮");这种方法还有其他的吗?
----------------解决方案--------------------------------------------------------
其实那就话显示出来了.......你移动下边框,他就全部出来了...至于原因...同求。
----------------解决方案--------------------------------------------------------
程序代码:
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ButtonEvent1 extends Frame{
public static void main(String[] args) {
new ButtonEvent1();
}
Button button=new Button("测试按钮");
Panel panel=new Panel();
Label label=new Label("");
public ButtonEvent1(){
super("按钮的事件处理");
this.setLayout(new FlowLayout());
// panel=(JPanel)this.getContentPane();
panel.add(button);
panel.add(label);
this.add(panel);
button.addActionListener(new MyEvent());//添加监听
setSize(250,120);
this.setVisible(true);
}
//内部类,处理单击事件
class MyEvent implements ActionListener{
public void actionPerformed(ActionEvent e) {
panel.setSize(250, 120); //panel内的东东变大了,所以panel也要重新指定大小
label.setSize(50, 50); //label内容变多了,所以需要重新指定大小
label.setText("你单击了按钮");
}
}
}
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ButtonEvent1 extends Frame{
public static void main(String[] args) {
new ButtonEvent1();
}
Button button=new Button("测试按钮");
Panel panel=new Panel();
Label label=new Label("");
public ButtonEvent1(){
super("按钮的事件处理");
this.setLayout(new FlowLayout());
// panel=(JPanel)this.getContentPane();
panel.add(button);
panel.add(label);
this.add(panel);
button.addActionListener(new MyEvent());//添加监听
setSize(250,120);
this.setVisible(true);
}
//内部类,处理单击事件
class MyEvent implements ActionListener{
public void actionPerformed(ActionEvent e) {
panel.setSize(250, 120); //panel内的东东变大了,所以panel也要重新指定大小
label.setSize(50, 50); //label内容变多了,所以需要重新指定大小
label.setText("你单击了按钮");
}
}
}
----------------解决方案--------------------------------------------------------
或者这样实现
程序代码:
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class ButtonEvent1 extends JFrame{
public static void main(String[] args) {
new ButtonEvent1();
}
Button button=new Button("测试按钮");
JPanel panel=new JPanel();
JLabel label=new JLabel("");
public ButtonEvent1(){
super("按钮的事件处理");
this.setLayout(new FlowLayout());
panel=(JPanel)this.getContentPane();
panel.add(button);
panel.add(label);
// this.add(panel);
button.addActionListener(new MyEvent());//添加监听
setSize(250,120);
this.setVisible(true);
}
//内部类,处理单击事件
class MyEvent implements ActionListener{
public void actionPerformed(ActionEvent e) {
// panel.setSize(250, 120);
// label.setSize(50, 50);
label.setText("你单击了按钮");
}
}
}
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class ButtonEvent1 extends JFrame{
public static void main(String[] args) {
new ButtonEvent1();
}
Button button=new Button("测试按钮");
JPanel panel=new JPanel();
JLabel label=new JLabel("");
public ButtonEvent1(){
super("按钮的事件处理");
this.setLayout(new FlowLayout());
panel=(JPanel)this.getContentPane();
panel.add(button);
panel.add(label);
// this.add(panel);
button.addActionListener(new MyEvent());//添加监听
setSize(250,120);
this.setVisible(true);
}
//内部类,处理单击事件
class MyEvent implements ActionListener{
public void actionPerformed(ActionEvent e) {
// panel.setSize(250, 120);
// label.setSize(50, 50);
label.setText("你单击了按钮");
}
}
}
----------------解决方案--------------------------------------------------------
大小问题……这就不叫问题,设置一下打小
----------------解决方案--------------------------------------------------------
不想用JLabel已经给定窗口大小了,不用改变窗口的大小,怎么把“你单击了按钮”这句话显示出来?帮帮忙,谢谢!
----------------解决方案--------------------------------------------------------
布局的问题
程序代码:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
@SuppressWarnings("serial")
public class ButtonEvent extends Frame{
public static void main(String[] args) {
new ButtonEvent();
}
Button button=new Button("测试按钮");
Panel panel=new Panel();
Label label=new Label();
public ButtonEvent(){
panel.setLayout(new GridLayout());
panel.add(button);
panel.add(label);
this.add(panel,BorderLayout.SOUTH);
button.addActionListener(new MyEvent());//添加监听
setSize(300,90);
this.setVisible(true);
}
//内部类,处理单击事件
class MyEvent implements ActionListener{
public void actionPerformed(ActionEvent e) {
label.setText("你单击了按钮");
}
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
@SuppressWarnings("serial")
public class ButtonEvent extends Frame{
public static void main(String[] args) {
new ButtonEvent();
}
Button button=new Button("测试按钮");
Panel panel=new Panel();
Label label=new Label();
public ButtonEvent(){
panel.setLayout(new GridLayout());
panel.add(button);
panel.add(label);
this.add(panel,BorderLayout.SOUTH);
button.addActionListener(new MyEvent());//添加监听
setSize(300,90);
this.setVisible(true);
}
//内部类,处理单击事件
class MyEvent implements ActionListener{
public void actionPerformed(ActionEvent e) {
label.setText("你单击了按钮");
}
}
}
----------------解决方案--------------------------------------------------------