Button/JPanel 网格显示不正确
Button/JPanel grids don't appear correctly
我有 3 个 class。
我想创建一个 JPanel
的 3x3 阵列网格。
每个 JPanel
包含一个 JButton
的 3x3 阵列网格。 (一共是81个JButton
像数独)
问题是当我执行我的程序时,它并没有显示所有的 JPanel
s,只出现了几个 JPanel
s(和它们的 JButton
s) ...
这是我的 3 classes 的代码,我找不到问题出在哪里...
Main
class:
import javax.swing.JFrame;
public class Main {
public static Griglia g[][] = new Griglia[3][3];
public static void main(String[] args) {
JFrame f = new JFrame();
Listener lis = new Listener();
for(int i=0; i<3; i++)
{
for(int ii=0; ii<3; ii++)
{
g[i][ii]=new Griglia();
g[i][ii].setLocation(i*155,ii*155);
g[i][ii].repaint();
g[i][ii].addMouseListener(lis);
f.add(g[i][ii]);
g[i][ii].setVisible(true);
g[i][ii].repaint();
f.repaint();
}
}
f.setSize(470,500);
f.setVisible(true);
}
}
Griglia
class:
import java.awt.*;
import javax.swing.*;
public class Griglia extends JPanel{
Casella c[][] = new Casella[3][3];
int flag=0;
public Griglia(){
this.setSize(150,150);
this.setLayout(null);
this.setBackground(Color.black);
for(int i=0; i<3; i++)
{
for(int ii=0; ii<3; ii++)
{
c[i][ii]=new Casella();
c[i][ii].setLocation(i*50, ii*50);
c[i][ii].repaint();
this.add(c[i][ii]);
this.repaint();
}
}
}
}
Casella
class:
import java.awt.Color;
import javax.swing.JButton;
public class Casella extends JButton{
int flag=0;
public Casella(){
this.setSize(50,50);
this.setBackground(Color.LIGHT_GRAY);
this.setVisible(true);
}
}
您的代码的最基本问题是缺乏对布局管理 API 工作原理的理解。
对于初学者,JFrame
默认使用 BorderLayout
,这意味着在您的情况下,只会显示最后添加的组件。
尝试依赖 null
布局是不明智的,并且会给您带来无穷无尽的问题 运行。
更好的解决方案是找到一个或多个布局管理器来帮助您实现您的解决方案,例如GridLayout
先看看 Laying Out Components Within a Container and How to Use GridLayout
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Main {
public static Griglia g[][] = new Griglia[3][3];
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(3, 3));
for (int i = 0; i < 3; i++) {
for (int ii = 0; ii < 3; ii++) {
g[i][ii] = new Griglia();
// g[i][ii].setLocation(i * 155, ii * 155);
// g[i][ii].repaint();
// g[i][ii].addMouseListener(lis);
frame.add(g[i][ii]);
// g[i][ii].setVisible(true);
// g[i][ii].repaint();
// f.repaint();
}
}
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
/////////////////////////////////////////////////////////////////////
public static class Griglia extends JPanel {
Casella c[][] = new Casella[3][3];
int flag = 0;
public Griglia() {
// this.setSize(150, 150);
// this.setLayout(null);
this.setBackground(Color.black);
setLayout(new GridLayout(3, 3));
for (int i = 0; i < 3; i++) {
for (int ii = 0; ii < 3; ii++) {
c[i][ii] = new Casella();
// c[i][ii].setLocation(i * 50, ii * 50);
// c[i][ii].repaint();
this.add(c[i][ii]);
// this.repaint();
}
}
}
}
////////////////////////////////////////////////////////////////////////////
public static class Casella extends JButton {
int flag = 0;
public Casella() {
// this.setSize(50, 50);
this.setBackground(Color.LIGHT_GRAY);
// this.setVisible(true);
}
}
}
我有 3 个 class。
我想创建一个 JPanel
的 3x3 阵列网格。
每个 JPanel
包含一个 JButton
的 3x3 阵列网格。 (一共是81个JButton
像数独)
问题是当我执行我的程序时,它并没有显示所有的 JPanel
s,只出现了几个 JPanel
s(和它们的 JButton
s) ...
这是我的 3 classes 的代码,我找不到问题出在哪里...
Main
class:
import javax.swing.JFrame;
public class Main {
public static Griglia g[][] = new Griglia[3][3];
public static void main(String[] args) {
JFrame f = new JFrame();
Listener lis = new Listener();
for(int i=0; i<3; i++)
{
for(int ii=0; ii<3; ii++)
{
g[i][ii]=new Griglia();
g[i][ii].setLocation(i*155,ii*155);
g[i][ii].repaint();
g[i][ii].addMouseListener(lis);
f.add(g[i][ii]);
g[i][ii].setVisible(true);
g[i][ii].repaint();
f.repaint();
}
}
f.setSize(470,500);
f.setVisible(true);
}
}
Griglia
class:
import java.awt.*;
import javax.swing.*;
public class Griglia extends JPanel{
Casella c[][] = new Casella[3][3];
int flag=0;
public Griglia(){
this.setSize(150,150);
this.setLayout(null);
this.setBackground(Color.black);
for(int i=0; i<3; i++)
{
for(int ii=0; ii<3; ii++)
{
c[i][ii]=new Casella();
c[i][ii].setLocation(i*50, ii*50);
c[i][ii].repaint();
this.add(c[i][ii]);
this.repaint();
}
}
}
}
Casella
class:
import java.awt.Color;
import javax.swing.JButton;
public class Casella extends JButton{
int flag=0;
public Casella(){
this.setSize(50,50);
this.setBackground(Color.LIGHT_GRAY);
this.setVisible(true);
}
}
您的代码的最基本问题是缺乏对布局管理 API 工作原理的理解。
对于初学者,JFrame
默认使用 BorderLayout
,这意味着在您的情况下,只会显示最后添加的组件。
尝试依赖 null
布局是不明智的,并且会给您带来无穷无尽的问题 运行。
更好的解决方案是找到一个或多个布局管理器来帮助您实现您的解决方案,例如GridLayout
先看看 Laying Out Components Within a Container and How to Use GridLayout
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Main {
public static Griglia g[][] = new Griglia[3][3];
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(3, 3));
for (int i = 0; i < 3; i++) {
for (int ii = 0; ii < 3; ii++) {
g[i][ii] = new Griglia();
// g[i][ii].setLocation(i * 155, ii * 155);
// g[i][ii].repaint();
// g[i][ii].addMouseListener(lis);
frame.add(g[i][ii]);
// g[i][ii].setVisible(true);
// g[i][ii].repaint();
// f.repaint();
}
}
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
/////////////////////////////////////////////////////////////////////
public static class Griglia extends JPanel {
Casella c[][] = new Casella[3][3];
int flag = 0;
public Griglia() {
// this.setSize(150, 150);
// this.setLayout(null);
this.setBackground(Color.black);
setLayout(new GridLayout(3, 3));
for (int i = 0; i < 3; i++) {
for (int ii = 0; ii < 3; ii++) {
c[i][ii] = new Casella();
// c[i][ii].setLocation(i * 50, ii * 50);
// c[i][ii].repaint();
this.add(c[i][ii]);
// this.repaint();
}
}
}
}
////////////////////////////////////////////////////////////////////////////
public static class Casella extends JButton {
int flag = 0;
public Casella() {
// this.setSize(50, 50);
this.setBackground(Color.LIGHT_GRAY);
// this.setVisible(true);
}
}
}