По какой -то причине, когда я переключил свое текстовое поле для Textarea, он занял размер всего окна, и я должен максимизировать окно, чтобы увидеть самые левые компоненты, и изменение размера кадра только изменяет его ширину, и он по -прежнему потребляет всю ширину окна. Есть ли способ исправить это? Программы с Textarea, повышенная ширина рамки < /p>
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class GUI implements ActionListener{
// Defines some components for use in all classes and methods.
private JFrame frame = new JFrame();
private JPanel panel = new JPanel();
private GridBagLayout gbl = new GridBagLayout();
private GridBagConstraints gcon = new GridBagConstraints();
// The GUI method, where all the work to create the GUI happens.
public GUI() {
// Defines 15 different buttons.
TextField comp1 = new TextField();
Label comp2 = new Label("d", Label.CENTER);
TextField comp3 = new TextField();
Checkbox comp4 = new Checkbox();
Label comp5 = new Label("Success");
TextField comp6 = new TextField();
Checkbox comp7 = new Checkbox();
Label comp8 = new Label("Exploding crits");
Checkbox comp9 = new Checkbox();
Label comp10 = new Label("Double crits");
Checkbox comp11 = new Checkbox();
Label comp12 = new Label("Compute");
Choice comp13 = new Choice();
comp13.add("+");
comp13.add("-");
comp13.add("*");
comp13.add("/");
Button comp14 = new Button("Roll");
Button comp15 = new Button("Clear");
TextArea comp16 = new TextArea("Output");
// comp16.setEditable(false);
int border = 25;
// Sets the border and layout of the panel.
panel.setBorder(BorderFactory.createEmptyBorder(border, border, border, border));
panel.setLayout(gbl);
// Sets the gcon settings that apply to all components, like weight and how it'll fill the screen.
gcon.weightx = 1;
gcon.weighty = 1;
gcon.fill = GridBagConstraints.BOTH;
// Creates an array with lots of buttons and the spaces they occupy. if the same button occupies 2 indexes, it's because it's a larger button. In a later version of this program, the buttons will be switched with different components. It is then sent into a method which adds them to the panel in a formated way according to how they've been placed in the array.
Component[] buttonArray = {comp1, comp2, comp3, comp4, comp5, comp6, comp7, comp8, comp8, comp9, comp10, comp10, comp11, comp12, comp13};
componentFormater(buttonArray, 3);
// Creates arrays for the last 3 components and their positions + dimensions for feeding into a method that manually adds their widths, heights and positions.
Component[] buttonArray2 = {comp14, comp15, comp16};
int[][] positions = {
{0, 0, 1, 1},
{0, 1, 1, 1},
{1, 0, 2, 2}
};
manualComponentFormater(buttonArray2, buttonArray, positions);
// Adds the panel to the frame and changes some frame settings before setting it to be visible.
frame.setSize(180+border*2, 480+border*2);
frame.setLocationRelativeTo(null);
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Dice Roller");
// frame.pack();
// frame.setExtendedState(Frame.MAXIMIZED_BOTH);
frame.setVisible(true);
}
// A method for manually formating components, using an array of components to format, an array of previously formated components, and an array of the positions and dimensions of the components.
private void manualComponentFormater(Component[] components, Component[] previousComponents, int[][] positions) {
for (int i = 0; i
Подробнее здесь: https://stackoverflow.com/questions/796 ... indow-size