私は自分のスイング計算機を作ってボタンを設定しているときに、各ボタンは均等に配置されていますが、最後に追加するボタンは常に画面全体ここでボタンを設定する方法があります。私はスイングで電卓を作っていますが、ボタンはすべて台無しになっています
private void setupRelations() {
window.setJMenuBar(menuBar);
window.setLayout(new BorderLayout());
window.add(inputPanel, BorderLayout.NORTH);
window.add(buttonsPanel);
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
copyResult.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
StringSelection selection = new StringSelection(input.getText());
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(selection, null);
}
});
pasteInput.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String result = "";
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable contents = clipboard.getContents(null);
boolean hasTransferableText = (contents != null) &&
contents.isDataFlavorSupported(DataFlavor.stringFlavor);
if (hasTransferableText) {
try {
result = (String)contents.getTransferData(DataFlavor.stringFlavor);
} catch (UnsupportedFlavorException ex){
System.out.println(ex);
ex.printStackTrace();
} catch (IOException ex) {
System.out.println(ex);
ex.printStackTrace();
}
}
input.setText(result);
}
});
windowOperations.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
specialOperations.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
file.add(copyResult);
file.add(pasteInput);
file.add(exit);
windows.add(windowOperations);
windows.add(specialOperations);
menuBar.add(file);
menuBar.add(windows);
inputPanel.setLayout(new BorderLayout());
input.setEditable(false);
input.setText("sss");
input.setHorizontalAlignment(JTextField.CENTER);
input.setFont(font);
inputPanel.add(input);
buttonsPanel.setLayout(new BorderLayout());
buttonsPanel.setBounds(window.getX() + (75 - 65), window.getY() + 13, 500, 500);
int sLength = 65;
int padding = 75;
for(int i = 0; i < buttonNames[i].length; i++) {
for(int j = 0; j < buttonNames.length; j++) {
JButton button = new JButton(buttonNames[j][i]);
button.setBounds(buttonsPanel.getX() + i * padding,
buttonsPanel.getY() + j * padding, sLength, sLength);
button.addActionListener(getActionListener(i, j));
buttonsPanel.add(button);
}
}
}
getActionListener方法は、ボタンのみのためcorrospondingアクションを取得するためである誰かが私を助けることができますか?
ヒント:レイアウトに関する質問があります。レイアウトの周りにないコードは避けてください。言い換えれば:http://stackoverflow.com/help/mcveを見て、あなたのボタンがどのように「乱される」かを示す例を書き留めてみてください。 – GhostCat