프로그램에서 필자는 10 개의 텍스트 필드를 세로로 정렬하려고합니다. 대신 나는이 얻을 :Swing GridBagLayout 연기 함
코드 여기 : 기본적으로
import java.awt.*;
import java.util.Random;
import javax.swing.*;
import java.awt.event.*;
public class Gsb extends JFrame {
JTextField word1;
JTextField word2;
JTextField word3;
JTextField word4;
JTextField word5;
JTextField word6;
JTextField word7;
JTextField word8;
JTextField word9;
JTextField word10;
JButton gofor;
JButton rego;
JTextArea Stuff;
JPanel panel;
JPanel texts;
JPanel buttons;
JButton Quit;
JPanel bigt;
String[] words = new String[20];
static int[] randnums = new int[20];
int i = 0;
public static void main(String[] args) {
new Gsb();
Random Rand = new Random();
int counter = 0;
int rand;
int[] pick = new int[20];
for(int i = 0; i < 20; i++)
{
rand = Rand.nextInt(20) + 1;
if (CheckForDuplicate(pick, rand, counter))
{
pick[i] = rand;
counter++;
}
else
{
i--;
}
}
for(int p = 0; p < 20; p++) {
randnums[p] = pick[p] - 1;
}
}
static boolean CheckForDuplicate(int[] array, int number, int counter)
{
for (int i = 0; i < counter; i++)
{
if (array[i] == number)
{
return false;
}
}
return true;
}
public Gsb() {
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension dim = tk.getScreenSize();
int xPos = (dim.width/2) - (this.getWidth()/2);
int yPos = (dim.height/2) - (this.getHeight()/2);
this.setLocation(xPos, yPos);
GridBagConstraints c = new GridBagConstraints();
GridBagConstraints d = new GridBagConstraints();
this.setSize(500, 500);
this.setTitle("checker");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
panel = new JPanel();
texts = new JPanel();
buttons = new JPanel();
bigt = new JPanel();
word1 = new JTextField(10);
word2 = new JTextField(10);
word3 = new JTextField(10);
word4 = new JTextField(10);
word5 = new JTextField(10);
word6 = new JTextField(10);
word7 = new JTextField(10);
word8 = new JTextField(10);
word9 = new JTextField(10);
word10 = new JTextField(10);
gofor = new JButton("BONANZA!");
rego = new JButton("Restart");
Stuff = new JTextArea(40, 40);
Quit = new JButton("Exit");
Stuff.setLineWrap(true);
Stuff.setWrapStyleWord(true);
Stuff.setText("welcome to the google search bonanza! first user write 10 random words and press BONANZA!");
JScrollPane useless = new JScrollPane(Stuff, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
panel.setLayout(new GridBagLayout());
texts.setLayout(new GridBagLayout());
buttons.setLayout(new GridBagLayout());
bigt.setLayout(new GridBagLayout());
c.fill = GridBagConstraints.CENTER;
d.fill = GridBagConstraints.RELATIVE;
d.gridx = 0;
d.gridy = 0;
texts.add(word1, d);
d.gridx = 0;
d.gridy = 1;
texts.add(word2, d);
d.gridx = 0;
d.gridy = 2;
texts.add(word3, d);
d.gridx = 0;
d.gridy = 3;
texts.add(word4, d);
d.gridx = 0;
d.gridy = 4;
texts.add(word5, d);
d.gridx = 0;
d.gridy = 5;
texts.add(word6, d);
d.gridx = 0;
d.gridy = 6;
texts.add(word7, d);
d.gridx = 0;
d.gridy = 7;
texts.add(word8, d);
d.gridx = 0;
d.gridy = 8;
texts.add(word9, d);
d.gridx = 0;
d.gridy = 9;
texts.add(word10);
c.gridx = 10;
c.gridy = 0;
buttons.add(gofor,c);
c.gridx = 9;
c.gridy = 0;
buttons.add(Quit,c);
c.gridx = 8;
c.gridy = 0;
buttons.add(rego ,c);
Bonanza bonanzer = new Bonanza();
Bonanzatwo bon2 = new Bonanzatwo();
Reset Reseter = new Reset();
Exit Exiter = new Exit();
rego.addActionListener(Reseter);
gofor.addActionListener(bonanzer);
gofor.addActionListener(bon2);
Quit.addActionListener(Exiter);
bigt.add(Stuff);
c.gridheight = 20;
c.gridx = 0;
c.gridy = 0;
panel.add(texts, c);
panel.add(buttons);
panel.add(bigt);
panel.add(useless);
this.add(panel);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
this.setUndecorated(true);
this.setVisible(true);
this.setTitle("The Google search bonanza");
}
을 분명히 아니었다면, 메신저는 다음과 같이가는 :
1) 더 나은 도움말을 빨리 게시 A [MCVE] 또는 [짧은, 자기 포함, 올바른 예] (http://www.sscce.org/). 2) 들여 쓰기 코드 행과 블록의 논리적이고 일관된 형식을 사용하십시오. 들여 쓰기는 코드의 흐름을 따라 가기 쉽도록 만들어졌습니다. 3)'Toolkit tk = Toolkit.getDefaultToolkit();을 대체하십시오. .. this.setLocation (xPos, yPos);'this.setLocationRelativeTo (null); '를 사용하여 같은 효과를냅니다. 또는 더 나은. 'this.setLocationByPlatform (true);'.. –
.. 4) 최소한의 크기로 GUI의 의도 된 * 레이아웃의 아스키 아트 또는 간단한 그림을 제공하고, ** 크기를 재조정 할 수 있다면 더 많은 너비와 높이를 제공하십시오. ** ** 후자 **는 여분의 공간을 할당하는 방법을 보여줍니다. –
감사합니다, 나는 다음 번에 그것을 염두에 두겠습니다! –