标题:[求助]我的程序哪里错了??
只看楼主
向着梦想加速
Rank: 1
等 级:新手上路
帖 子:82
专家分:0
注 册:2007-9-18
 问题点数:0 回复次数:4 
[求助]我的程序哪里错了??
为什么这个程序上按钮的文本看不见啊??请大家帮我看看怎么回事!!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class Counter extends JFrame
{
//实例化2个显示屏
JTextField Jtextfield1=new JTextField(20);
JTextField Jtextfield2=new JTextField(5);

//实例化4个面板
JPanel Jpanel1=new JPanel();
JPanel Jpanel2=new JPanel();
JPanel Jpanel3=new JPanel();
JPanel Jpanel4=new JPanel();

//实例化布局管理器
GridLayout GLayout3=new GridLayout(1,3);
GridLayout GLayout4=new GridLayout(4,5);

//实例化按钮
JButton[][] b=new JButton[][]{{new JButton("MC"),
new JButton("MR"),
new JButton("MS"),
new JButton("M+")},

{new JButton("Backspace"),
new JButton("CE"),
new JButton("C")},

{new JButton("0"),
new JButton("1"),
new JButton("2"),
new JButton("3"),
new JButton("4"),
new JButton("5"),
new JButton("6"),
new JButton("7"),
new JButton("8"),
new JButton("9"),
new JButton("/"),
new JButton("*"),
new JButton("-"),
new JButton("+"),
new JButton("sqrt"),
new JButton("%"),
new JButton("1/x"),
new JButton("="),
new JButton("+/-")}
};

ActionListener action=new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Jtextfield1.setText(" ");
}
};

public Counter(String str)
{
super(str);
setLayout(null);
setJPanel1();
setJPanel2();
add(Jpanel1);
add(Jpanel2);
for(int i=0;i<b.length;i++)
{
for(int j=0;j<b[i].length;j++)
{
b[i][j].addActionListener(action);
}
}
}
public void setJPanel1()
{
GridLayout GLayout1=new GridLayout(1,1);
Jpanel1.setLayout(GLayout1);
Jpanel1.add(Jtextfield1);
Jtextfield1.setHorizontalAlignment(JTextField.RIGHT );
Jpanel1.setBounds(10,3,252,22);
}
public void setJPanel2()
{
GridLayout GLayout2=new GridLayout(5,1,10,10);
Jpanel2.setLayout(GLayout2);
Jpanel2.add(b[0][0]);
Jpanel2.add(b[0][1]);
Jpanel2.add(b[0][2]);
Jpanel2.add(b[0][3]);
Jpanel2.setBounds(10,40,30,200);
}
public static void main(String[] args)
{
Counter counter=new Counter("计算器");
counter.setSize(280,250);
counter.setLocation(300,250);
counter.setVisible(true);
counter.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
搜索更多相关主题的帖子: 显示屏 import 
2007-11-18 00:43
xbw0114
Rank: 1
等 级:新手上路
帖 子:141
专家分:0
注 册:2007-8-8
得分:0 
以下是引用向着梦想加速在2007-11-18 0:43:47的发言:
为什么这个程序上按钮的文本看不见啊??请大家帮我看看怎么回事!!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class Counter extends JFrame
{
//实例化2个显示屏
JTextField Jtextfield1=new JTextField(20);
JTextField Jtextfield2=new JTextField(5);

//实例化4个面板
JPanel Jpanel1=new JPanel();
JPanel Jpanel2=new JPanel();
JPanel Jpanel3=new JPanel();
JPanel Jpanel4=new JPanel();

//实例化布局管理器
GridLayout GLayout3=new GridLayout(1,3);
GridLayout GLayout4=new GridLayout(4,5);

//实例化按钮
JButton[][] b=new JButton[][]{{new JButton("MC"),
new JButton("MR"),
new JButton("MS"),
new JButton("M+")},

{new JButton("Backspace"),
new JButton("CE"),
new JButton("C")},

{new JButton("0"),
new JButton("1"),
new JButton("2"),
new JButton("3"),
new JButton("4"),
new JButton("5"),
new JButton("6"),
new JButton("7"),
new JButton("8"),
new JButton("9"),
new JButton("/"),
new JButton("*"),
new JButton("-"),
new JButton("+"),
new JButton("sqrt"),
new JButton("%"),
new JButton("1/x"),
new JButton("="),
new JButton("+/-")}
};//感觉是这里的声明出了问题

ActionListener action=new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Jtextfield1.setText(" ");
}
};

public Counter(String str)
{
super(str);
setLayout(null);
setJPanel1();
setJPanel2();
add(Jpanel1);
add(Jpanel2);
for(int i=0;i<b.length;i++)
{
for(int j=0;j<b[i].length;j++)
{
b[i][j].addActionListener(action);
}
}
}
public void setJPanel1()
{
GridLayout GLayout1=new GridLayout(1,1);
Jpanel1.setLayout(GLayout1);
Jpanel1.add(Jtextfield1);
Jtextfield1.setHorizontalAlignment(JTextField.RIGHT );
Jpanel1.setBounds(10,3,252,22);
}
public void setJPanel2()
{
GridLayout GLayout2=new GridLayout(5,1,10,10);
Jpanel2.setLayout(GLayout2);
Jpanel2.add(b[0][0]);
Jpanel2.add(b[0][1]);
Jpanel2.add(b[0][2]);
Jpanel2.add(b[0][3]);
Jpanel2.setBounds(10,40,30,200);
}
public static void main(String[] args)
{
Counter counter=new Counter("计算器");
counter.setSize(280,250);
counter.setLocation(300,250);
counter.setVisible(true);
counter.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}


勤奋造就未来
2007-11-18 01:00
netstriker
Rank: 1
等 级:新手上路
威 望:1
帖 子:257
专家分:0
注 册:2007-3-24
得分:0 

只是一个布局设置问题,改一下就可以用了

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

class Counter extends JFrame
{
//实例化2个显示屏
JTextField Jtextfield1=new JTextField(20);
JTextField Jtextfield2=new JTextField(5);

//实例化4个面板
JPanel Jpanel1=new JPanel();
JPanel Jpanel2=new JPanel();
JPanel Jpanel3=new JPanel();
JPanel Jpanel4=new JPanel();

//实例化布局管理器
GridLayout GLayout3=new GridLayout(1,3);
GridLayout GLayout4=new GridLayout(4,5);

//实例化按钮
JButton[][] b=new JButton[][]{{new JButton("MC"),
new JButton("MR"),
new JButton("MS"),
new JButton("M+")},

{new JButton("Backspace"),
new JButton("CE"),
new JButton("C")},

{new JButton("0"),
new JButton("1"),
new JButton("2"),
new JButton("3"),
new JButton("4"),
new JButton("5"),
new JButton("6"),
new JButton("7"),
new JButton("8"),
new JButton("9"),
new JButton("/"),
new JButton("*"),
new JButton("-"),
new JButton("+"),
new JButton("sqrt"),
new JButton("%"),
new JButton("1/x"),
new JButton("="),
new JButton("+/-")}
};

ActionListener action=new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Jtextfield1.setText(" ");
}
};

public Counter(String str)
{
super(str);
setLayout(null);
setJPanel1();
setJPanel2();
this.setLayout(new BorderLayout());
add(Jpanel1,BorderLayout.NORTH); // 这里应该设置一个布局管理器,否则的话会把原来的JPanel 盖住了
add(Jpanel2,BorderLayout.WEST);
for(int i=0;i<b.length;i++)
{
// System.out.println("the length is "+b.length);
for(int j=0;j<b[i].length;j++)
{
b[i][j].addActionListener(action);
}
}
}
public void setJPanel1()
{
GridLayout GLayout1=new GridLayout(1,1);
Jpanel1.setLayout(GLayout1);
Jpanel1.add(Jtextfield1);
Jtextfield1.setHorizontalAlignment(JTextField.RIGHT );
Jpanel1.setBounds(10,3,252,22);
}
public void setJPanel2()
{
GridLayout GLayout2=new GridLayout(5,1,10,10);
Jpanel2.setLayout(GLayout2);
Jpanel2.add(b[0][0]);
Jpanel2.add(b[0][1]);
Jpanel2.add(b[0][2]);
Jpanel2.add(b[0][3]);
Jpanel2.setBounds(10,40,30,200);
}
public static void main(String[] args)
{
Counter counter=new Counter("计算器");
counter.setSize(280,250);
counter.setLocation(300,250);
counter.setVisible(true);
counter.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

2007-11-18 01:29
向着梦想加速
Rank: 1
等 级:新手上路
帖 子:82
专家分:0
注 册:2007-9-18
得分:0 
回复:(netstriker) 只是一个布局设置问题,改一下...
可是如果添加了一个BorderLayout布局管理器的话,那JPanel1和JPanel2就只能分布在东南西北这几个方向,我现在想利用setBounds()这个方法来达到自由摆放面板的功能!

2007-11-18 11:15
netstriker
Rank: 1
等 级:新手上路
威 望:1
帖 子:257
专家分:0
注 册:2007-3-24
得分:0 
   也可以分布在中间啊    BorderLayout.CENTER,  其实如果想这样用setBounds来定位的话,应该要重写LayoutManager接口的实现类.
2007-11-18 13:38



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-186864-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.550499 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved