java swing网格布局问题 我定义了一个6行10列的网格 想在里面添加60个按钮 代码如下

java swing网格布局问题 我定义了一个6行10列的网格 想在里面添加60个按钮 代码如下,第1张

你往panel里面加button的函数没有被调用,也就是init函数没有被执行,

你在main函数中new jdttest()只会调用jdttest类的构造函数public jdttest()

不会调用public void init()函数,所以button数组没有加到panel中,自然就没显示.

解决办法 你在构造函数public jdttest()中panel.setLayout(new GridLayout(6,10))后面

调用一下public void init()函数,就行了.

完整的Java程序如下(改动的地方见注释)

import java.awt.BorderLayout

import java.awt.GridLayout

import javax.swing.JButton

import javax.swing.JFrame

import javax.swing.JPanel

public class jdttest extends JFrame{

 JButton button[][]=new JButton[6][10]

 JPanel panel=new JPanel()

 public jdttest(){

  this.setSize(900,630)

  this.setLayout(new BorderLayout())

  panel.setLayout(new GridLayout(6,10))

  panel.setBounds(150, 50, 600,360)

  init()  //这里加一句调用init()函数

  this.add(panel,BorderLayout.CENTER)

  this.setVisible(true)

 }

 public void init(){

  for(int cols=0cols<6cols++){

   for(int rows=0rows<10rows++){

    button[cols][rows]=new JButton("7")

    panel.add(button[cols][rows])

   }

  }

 }

 public static void main(String[] args) {

  new jdttest()

 }

}

null layout不是很好,所有的东西都要自己定义,一旦窗口大小变化就要重新计算。我建议使用MigLayout。上手慢,但很强大。http://www.miglayout.com

而且,就算你要用内建的Layout Manager,还有一个GridBag Layout,比Grid Layout更强大一点。

接下来,那个边框,不是JSeparator, 而是一个边框。我猜是

Border result = BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "修改信息: ")

如果是我,使用MigLayout,以上代码还是比较简单的:

import java.awt.event.KeyEvent

import javax.swing.BorderFactory

import javax.swing.JButton

import javax.swing.JFrame

import javax.swing.JLabel

import javax.swing.JPanel

import javax.swing.JTextField

import javax.swing.SwingUtilities

import javax.swing.border.Border

import javax.swing.border.EtchedBorder

import net.miginfocom.swing.MigLayout

public class MyFrame extends JFrame {

    public MyFrame() {

        begin()

    }

    private void begin() {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

        setLayout(new MigLayout("insets 10, fill", "[]", "[]5[]"))

        

        //first panel

        JPanel first = new JPanel()

        

        //first这个panel有5像素的边缘,2列,3行。第一列右对齐,占40%宽度;第二列默认,左对齐,占据剩余所有空间。

        first.setLayout(new MigLayout("insets 5, fill", "[right, 40%]5[fill, grow]", "[]5[]5[]"))

        first.setOpaque(false)

        //这个面板的border有些特殊:createTitledBorder()方法的签名可以有两个:前一个是线的类型,后面一个是标题文本。

        Border result = BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "修改信息: ")

        first.setBorder(result)

        //把first加到contentPane里面

        add(first, "cell 0 0, grow")

        

        JLabel original = new JLabel("输入原密码: ")

        JLabel newPass = new JLabel("输入新密码: ")

        JLabel confirm = new JLabel("确认新密码: ")

        

        JTextField orig_field = new JTextField()

        JTextField new_field = new JTextField()

        JTextField confirm_field = new JTextField()

        

        //miglayout的核心就是网格排布。用坐标来定义添加元素的位置

        first.add(original, "cell 0 0, w 100!")      //add to col 0, line 0, min:pref:max width all set to 100

        first.add(newPass, "cell 0 1, w 100!")       //add to col 0, line 1

        first.add(confirm, "cell 0 2, w 100!")       //add to col 0, line 2

        

        first.add(orig_field, "cell 1 0, w 150!")    //add to col 1, line 0, min:pref:max width all set to 150

        first.add(new_field, "cell 1 1, w 150!")    //add to col 1, line 1

        first.add(confirm_field, "cell 1 2, w 150!")    //add to col 1, line 2

        

        //按钮面板

        JPanel buttons = new JPanel()

        buttons.setOpaque(false)

        //边缘为5像素;有两列,中间是10像素的间距,列内元素居中;有一行,行中上下对齐也是居中

        buttons.setLayout(new MigLayout("insets 5, fill", "[center]10[center]", "[center]"))

        JButton yes = new JButton("Y. 确定")

        //快捷键设为虚拟键Y,得到下划线效果

        yes.setMnemonic(KeyEvent.VK_Y)

        

        JButton quit = new JButton("Q. 退出")

        //快捷键设为虚拟键Q,得到下划线效果

        quit.setMnemonic(KeyEvent.VK_Q)

        

        //把yes按钮加到第一列第一行,min:pref:max的大小都设为100像素

        buttons.add(yes, "cell 0 0, w 100!")

        //把quit按钮加到第二列第一行,min:pref:max的大小都设为100像素

        buttons.add(quit, "cell 1 0, w 100!")

        

        //把按钮面板加到contentPane里面

        add(buttons, "cell 0 1, grow")

        pack()

        setBounds(0, 0, 500, 400)

        setLocationRelativeTo(null)

        setVisible(true)

    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {

            @Override

            public void run() {

                MyFrame frame = new MyFrame()

            }

        })

    }

}

效果是这样:

MigLayout还允许你使用辅助线来debug。把这一行:

first.setLayout(new MigLayout("insets 5, fill", "[right, 40%]5[fill, grow]", "[]5[]5[]"))

改成:

first.setLayout(new MigLayout("insets 5, fill,debug", "[right, 40%]5[fill, grow]", "[]5[]5[]"))

你就可以看到first面板里面的辅助线了。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11570174.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-17
下一篇2023-05-17

发表评论

登录后才能评论

评论列表(0条)

    保存