回 帖 发 新 帖 刷新版面

主题:[讨论]我的程序哪里错了!!

为什么这个程序上按钮的文本看不见啊??请大家帮我看看怎么回事!!
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,40,180);
      }
        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);
        }
}

回复列表 (共3个回复)

沙发

给予按钮显示文本的空间  "相对"  的太小了:
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);
        Jpanel2.setBounds(20, 80, 60, 400);

    }
改下就知道了

板凳


哦,不过如果把那些参数改掉的话,那么按钮将会变得很大的,现在我想按钮就照原先的大小,因为我想做一个“计算器”如果按钮太大是不行的!因为我参照别人的代码来写的,不过别人的按钮大小跟我的差不多,他的按钮也不至于看不到文本!

3 楼

GridLayout
public GridLayout(int rows,
                  int cols,
                  int hgap,
                  int vgap)创建具有指定行数和列数的网格布局。给布局中的所有组件分配相等的大小。 
此外,将水平和垂直间距设置为指定值。水平间距将置于列与列之间。将垂直间距将置于行与行之间。 

rows 和 cols 中的一个可以为零(但不能两者同时为零),这表示可以将任何数目的对象置于行或列中。 

所有 GridLayout 构造方法都服从这一规定。 


参数:
rows - 该 rows 具有表示任意行数的值零
cols - 该 cols 具有表示任意列数的值零
hgap - 水平间距
vgap - 垂直间距

setBounds
public void setBounds(int x,
                      int y,
                      int width,
                      int height)移动组件并调整其大小。由 x 和 y 指定左上角的新位置,由 width 和 height 指定新的大小。 

参数:
x - 组件的新 x 坐标
y - 组件的新 y 坐标
width - 组件的新 width
height - 组件的新 height


public void setJPanel2() {
        //GridLayout GLayout2 = new GridLayout(5, 1, 10, 10);
        GridLayout GLayout2 = new GridLayout(4, 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);
        Jpanel2.setBounds(10, 40, 55, 150);
    }

自己多试几次,改变参数值,看效果怎样

我来回复

您尚未登录,请登录后再回复。点此登录或注册