自定义的JPanel带有JButtons,在JFrame中将它们叠放在一起显示。

huangapple go评论64阅读模式
英文:

Custom JPanel with JButtons displays them one on top of another in JFrame

问题

When I run the provided code, instead of a panel containing three buttons next to each other, I get a tiny panel with all the buttons stacked on top of each other. This issue occurs when the code is split into classes.

英文:

Basically, when i run the following code, instead of a panel containing three buttons next to each other i get a tiny panel that (i'm assuming) has all the buttons on top of each other. What's weird is that when i put all the code inside Main() it seems to work just fine, i can even add more buttons to myTopLeftPanel from main and they are displayed correctly, but when i split it into classes it starts acting weird. What's the cause of this? How can i fix it?

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

public class Main extends JFrame {
    int height = 500;
    int width = (int)Math.round(height*1.61803);
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new Main());
    }
    public Main() {
        this.setLayout(new BorderLayout());
        MyTopLeftPanel myTopLeftPanel = new MyTopLeftPanel();
        JPanel top = addPanel(new BorderLayout());
        top.add(myTopLeftPanel, BorderLayout.WEST);
        this.add(top, BorderLayout.NORTH);
        setup();
    }
    void setup(){
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(width, height);
        this.setVisible(true);
    }
    JPanel addPanel(LayoutManager layout){
        JPanel temp = new JPanel();
        temp.setLayout(layout);
        return temp;
    }
}
class MyButtonPanel extends JPanel{
    JButton[] tab;
    public MyButtonPanel(int numOfButtons, LayoutManager layout) {
        this.tab = new JButton[numOfButtons];
        Arrays.fill(tab, new JButton());
        this.setLayout(layout);
    }
}
class MyTopLeftPanel extends MyButtonPanel{
    public MyTopLeftPanel(){
        super(3, new FlowLayout());
        tab[0].setBackground(Color.RED);
        tab[0].setText("R");
        tab[1].setBackground(Color.GREEN);
        tab[1].setText("G");
        tab[2].setBackground(Color.BLUE);
        tab[2].setText("B");
        this.setLayout(new FlowLayout());
        this.add(tab[0], FlowLayout.LEFT);
        this.add(tab[1], FlowLayout.LEFT);
        this.add(tab[2], FlowLayout.LEFT);
    }
}

答案1

得分: 1

问题是Arrays.fill(tab, new JButton());

它只创建一个JButton实例,并将其分配给数组的每个位置。

这应该修复它:

class MyButtonPanel extends JPanel {
    JButton[] tab;
    public MyButtonPanel(int numOfButtons, LayoutManager layout) {
        this.tab = new JButton[numOfButtons];
        for (int i = 0; i < numOfButtons; i++) {
            tab[i] = new JButton();
        }
        this.setLayout(layout);
    }
}
英文:

The problem is Arrays.fill(tab, new JButton());.

It creates only one instance of JButton which is assigned to each position of the array.

This should fix it:

class MyButtonPanel extends JPanel{
    JButton[] tab;
    public MyButtonPanel(int numOfButtons, LayoutManager layout) {
        this.tab = new JButton[numOfButtons];
        for (int i = 0; i &lt; numOfButtons; i++) {
            tab[i] = new JButton();
        }
        this.setLayout(layout);
    }
}

huangapple
  • 本文由 发表于 2023年5月22日 23:09:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76307574.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定