당신의 주된 문제는 당신이 널 레이아웃과 절대 위치를 사용하고 있다는 것이다. 괜찮은 레이아웃 관리자를 사용한다면, 그들은 당신을 위해 이것을 처리 할 것입니다. 또한 모두 플랫폼 및 화면 해상도에서 잘 보이는 GUI를 훨씬 쉽게 유지 관리 할 수 있습니다. 예를 들어,
,
import java.awt.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class LayoutEg extends JPanel {
public LayoutEg() {
setBorder(BorderFactory.createTitledBorder("Personal Information"));
setLayout(new GridBagLayout());
addLabelWithComponent("First Name:", new JTextField(10), 0, 0);
addLabelWithComponent("Last Name:", new JTextField(10), 0, 1);
addLabelWithComponent("City:", new JTextField(10), 0, 2);
addLabelWithComponent("Street:", new JTextField(10), 0, 3);
addLabelWithComponent("Province:",
new JComboBox<>(new String[] { "Foo", "Bar" }), 0, 4);
addLabelWithComponent("Home Phone:", new JTextField(10), 2, 0);
addLabelWithComponent("Cell Phone:", new JTextField(10), 2, 1);
addLabelWithComponent("Email Address:", new JTextField(10), 2, 2);
addLabelWithComponent("Age", new JSpinner(
new SpinnerNumberModel(18, 17, 100, 1)), 2, 3);
JPanel radioPanel = new JPanel(new GridLayout(1, 0));
radioPanel.add(new JRadioButton("Male"));
radioPanel.add(new JRadioButton("Female"));
addLabelWithComponent("Gender:", radioPanel, 2, 4);
}
private void addLabelWithComponent(String text, JComponent component, int x, int y) {
addLabel(new JLabel(text), x, y);
addComponent(component, x + 1, y);
}
private void addComponent(JComponent component, int x, int y) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(5, 5, 5, 5);
add(component, gbc);
}
private void addLabel(JComponent component, int x, int y) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.BOTH;
int left = x == 2 ? 35 : 5;
gbc.insets = new Insets(5, left, 5, 5);
add(component, gbc);
}
private static void createAndShowGui() {
LayoutEg mainPanel = new LayoutEg();
JFrame frame = new JFrame("LayoutEg");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
당신은 단순히 당신의 JRadioButtons 더 큰를 만든 다음 다른 플랫폼에서이 응용 프로그램을 실행하고 이름이 사라질 경우, 무엇을? 그게 레이아웃 때문입니다.
예를 들어
, 위의 디스플레이와 같은 :
내가 그렇게 소스 코드 나에게 좋아 보이는 저 – Bauer
에 쉽게 갈 애플릿 자바를 만드는 슈퍼 새로운 오전 ... 지역이라고 할 수 있음 너무 작아서 Java가 레이블을 취소합니다. – exception1
더 나은 도움을 더 빨리 얻으려면 [MCTaRE] (http://stackoverflow.com/help/mcve)를 게시하십시오 (테스트 완료된 최소 예제). –