2014-02-22 5 views
1

나는 내 JRadioButtons 레이블이 "..."이고 원하는 텍스트가 아닙니다. 가 표시되는 이유

This IS what Im getting

ButtonGroup operations = new ButtonGroup(); 

JRadioButton[] buttons = new JRadioButton[2]; 

String[] buttonLabels = {"Male", "Female"}; 

for (int i=0; i<2; i++) { 
    buttons[i] = new JRadioButton(buttonLabels[i], true); 

    buttons[i].setLocation(400 + (i * 50) , 170); 
    buttons[i].setSize(35, 30); 

    add(buttons[i]); 
    operations.add(buttons[i]); 


} 
내가 버튼 [0] 남성을 말하고 싶다 "..."각 버튼을 내가 원하는 텍스트를 표시하지 않는 이유를 알고하지 않습니다 및 단추 1 말을 여성

+0

내가 그렇게 소스 코드 나에게 좋아 보이는 저 – Bauer

+1

에 쉽게 갈 애플릿 자바를 만드는 슈퍼 새로운 오전 ... 지역이라고 할 수 있음 너무 작아서 Java가 레이블을 취소합니다. – exception1

+0

더 나은 도움을 더 빨리 얻으려면 [MCTaRE] (http://stackoverflow.com/help/mcve)를 게시하십시오 (테스트 완료된 최소 예제). –

답변

8

전체 텍스트를 표시 할 공간이 충분하지 않은 경우 발생합니다. 서로의 아래에 놓거나 폭을 약간 늘리십시오.

+1

저는 LayoutManager가 이것을 처리 할 것이라고 생각합니다. layoutmanager가 화면에 맞출 수있는 방법을 알 것입니다. – nachokk

+0

@nachokk 정상적으로 생각하겠습니다 만,이 경우 크기가 수동으로 설정되므로 레이아웃 관리자는 말한 것 이상으로 스트레칭을 할 수 없습니다. – Mateusz

+1

나 자신을 바로 잡는다, 그는 널 레이아웃을 사용하지 않는 레이아웃 매니저를 사용해야한다 – nachokk

3

JRadioButton 너비를 늘려야합니다.

+0

버튼이 레이아웃 관리자의 제어하에 있다면'setSize'는 효과가 없습니다. 그들은 있어야하지만, 그것은 또 다른 호언 장담이다. 'setSize'와'setLoaction'은 피해야하며 대략적인 레이아웃 매니저가 사용되어야합니다. 이것은 OP가'JFrame # pack '을 사용하여 전체 컨테이너의 선호 크기를 고려하여 프레임의 초기 크기를 설정하여 문제를 자동으로 해결할 수있게합니다. 또한 서로 다른 글꼴 메트릭을 다른 시스템에 포함시키는 문제를 해결하여 응용 프로그램의 이식성을 향상시킵니다. – MadProgrammer

+0

입력 내용을 기반으로 제안을 제거했습니다. – ltalhouarne

0

밖으로 나왔다. 세트 크기가 더 크다

7

당신의 주된 문제는 당신이 널 레이아웃과 절대 위치를 사용하고 있다는 것이다. 괜찮은 레이아웃 관리자를 사용한다면, 그들은 당신을 위해 이것을 처리 할 것입니다. 또한 모두 플랫폼 및 화면 해상도에서 잘 보이는 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 더 큰를 만든 다음 다른 플랫폼에서이 응용 프로그램을 실행하고 이름이 사라질 경우, 무엇을? 그게 레이아웃 때문입니다.

예를 들어

, 위의 디스플레이와 같은 :

enter image description here

+0

OP가 '널 (null)'레이아웃을 사용하고 있다는 증거는 없습니다."출력으로 레이아웃 관리자가 사용 중임을 알게 될 것입니다. 단지 그것을 이해하지 못합니다.하지만 대답을 바꾸지는 않습니다.'pack'을 사용하는 대신 임의로 창 크기를 설정하는 경향이 큽니다. .. + 1 – MadProgrammer