Код: Выделить всё
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
public class CalculatorFrame extends JPanel {
int boardWidth;
int boardHeight;
public CalculatorFrame(int boardWidth, int boardHeight) {
this.boardWidth = boardWidth;
this.boardHeight = boardHeight;
setPreferredSize(new Dimension(this.boardWidth, this.boardHeight));
setBackground(Color.black);
setFocusable(true);
//Panel
JPanel jPanel = new JPanel();
jPanel.setPreferredSize(new Dimension(boardWidth, boardHeight));
jPanel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = 5;
c.gridheight = 4;
c.weightx = 1;
c.weighty = 1;
c.anchor = GridBagConstraints.FIRST_LINE_START;
jPanel.setVisible(true);
add(jPanel);
//Number display
JLabel label1 = new JLabel("Test", SwingConstants.RIGHT);
label1.setFont(new Font("Serif", Font.PLAIN, 80));
label1.setBackground(Color.white);
label1.setOpaque(true);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridheight = 1;
c.gridx = 0;
c.gridy = 4;
jPanel.add(label1, c);
//Display number: 1
JButton button1 = new JButton("1");
c.fill = GridBagConstraints.NONE ;
c.gridwidth = 1;
c.gridheight = 1;
c.gridx = 0;
c.gridy = 3;
button1.setVisible(true);
jPanel.add(button1, c);
}
}
Код: Выделить всё
import javax.swing.*;
import java.awt.*;
public class CalculatorProgram {
public static void main(String[] args) {
int boardWidth = 700;
int boardHeight = 800;
JFrame frame = new JFrame("Snake");
javax.swing.SwingUtilities.invokeLater(() -> {
frame.setVisible(true);
});
frame.setSize(boardWidth, boardHeight);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new FlowLayout());
CalculatorFrame calculatorFrame = new CalculatorFrame(boardWidth, boardHeight);
frame.add(calculatorFrame);
frame.pack();
frame.requestFocus();
}
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... dbaglayout
Мобильная версия