我将JPanel拆分为3个单元格。 在16个粒子的每一个中,每个细胞的左侧面板上都有Flure。 Flyr位于每个单元格的右侧面板上,采用4乘4格。
每当我设置边框时,我都无法让按钮(Flure和Flyr)不调整大小。只要按下按钮,就会设置边框。我认为这与我如何使用gridBagConstraints有关,或者它可能是我没有设置的大小。
import javax.swing.*;
import java.awt.*;
class Cell extends JButton {
private static final int NUMPARTICLES = 16;
private JPanel leftPanel;
private JPanel rightPanel;
public Cell(int number) {
setLayout(new GridBagLayout());
initFields();
initParticles();
initPanels();
if (number == 1)
this.setBackground(Color.red);
if (number == 2)
this.setBackground(Color.blue);
if (number == 3)
this.setBackground(Color.green);
}
private void initFields(){
this.leftPanel = new JPanel(new GridBagLayout());
this.rightPanel = new JPanel(new GridBagLayout());
}
private void initParticles(){
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
for(int i=0;i<NUMPARTICLES;i++){
gbc.gridy = i;
leftPanel.add(new Particle(),gbc);
}
}
private void initPanels(){
initButtons();
splitPane();
}
private void initButtons(){
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.ipadx = 10;
gbc.ipady = 30;
for(int i=0;i<4;i++){
gbc.gridx = i+2;
for(int j=0;j<NUMPARTICLES/4;j++){
gbc.gridy = j+2;
rightPanel.add(new Flure(),gbc);
}
}
}
private void splitPane(){
JSplitPane sp = new
JSplitPane(JSplitPane.HORIZONTAL_SPLIT,leftPanel,rightPanel);
sp.setEnabled(false);
sp.setResizeWeight(0.5);
this.add(sp);
}
}
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
public class Particle extends JButton {
public Particle(){
this.setEnabled(false);
this.setBackground(Color.gray);
this.setForeground(Color.white);
this.setLayout(new GridBagLayout());
initButtons();
}
private void initButtons(){
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
this.add(new Flyr(),gbc);
gbc.gridx = 1;
this.add(new Flyr(),gbc);
gbc.gridx = 2;
JButton flurSign = new JButton();
flurSign.setEnabled(false);
flurSign.setBackground(Color.black);
flurSign.setBackground(Color.blue);
this.add(flurSign,gbc);
}
}
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Flyr extends JButton implements MouseListener {
private static final LineBorder LB = new LineBorder(Color.magenta, 3);
public Flyr(){
setMargin(new Insets(15,15,15,15));
setBackground(Color.black);
setForeground(Color.white);
addMouseListener(this);
}
public void mouseClicked(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){
setBorder(LB);
}
}
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Flure extends JButton implements MouseListener {
private static final LineBorder LB = new LineBorder(Color.green, 3);
public Flure() {
setBackground(Color.black);
setForeground(Color.white);
setText("UP");
addMouseListener(this);
}
public void mouseClicked(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
setBorder(LB);
}
}
Publish -> Create New App Service