2012-04-08 4 views
3

내 gui의 레이아웃에 두 가지 문제가 있습니다. JTabbedPane을 사용하여 두 개의 JPanel을 보유하고 있으며, 각 패널에는 버튼과 텍스트 영역이 있으며 GridBagLayout을 사용하여 레이아웃되어 있습니다. 내 패널 중 하나에서 JTextArea를 사용하는 JScrollPane이 있습니다. 이 텍스트 영역에 아무 것도 추가 한 다음 gui를 클릭하여 더 이상 포커스가 없도록하거나 탭을 변경하면 모든 텍스트 필드와 텍스트의 크기가 가능한 작게 변경됩니다.키보드 초점이 맞지 않으면 스윙 구성 요소의 크기가 조절됩니다.

더 여기, 내 문제를 설명하기 전에 내가 텍스트 영역에 추가 한 후 GUI를 클릭하면 사진을 쫓고하려면 여기 Before I click off the gui

After I click off the gui

내가 추가하는 데 사용하는 코드는 패널에 JTextArea에 :

0,123,516 : 여기

table = new JTextArea(); 
    table.setEditable(false); 
    JScrollPane sp = new JScrollPane(table); 
    sp.setSize(40, 10); 
    c.insets = new Insets(10,10,10,10); 
    c.gridx = 1; 
    c.gridwidth = 4; 
    c.gridy = 7; 
    c.gridheight = 7; 
    this.add(sp, c); 

그리고 내가 패널에 텍스트 영역을 추가하는 데 사용하는 코드입니다

title = new JTextField(10); 
    author = new JTextField(10); 
    dueDate = new JTextField(10); 
    setDate = new JTextField(10); 
    setWeighting = new JTextField(10); 

    c.gridx = 2; 
    c.gridy = 1; 
    this.add(title, c);//add title field 
    c.gridx = 2; 
    c.gridy = 2; 
    this.add(author, c);//add author field 
    c.gridx = 2; 
    c.gridy = 3; 
    this.add(dueDate, c);//add dueDate field 
    c.gridx = 2; 
    c.gridy = 4; 
    this.add(setDate, c);//add setDate field 
    c.gridx = 2; 
    c.gridy = 5; 
    this.add(setWeighting, c);//add set Weighting field 
+0

컴파일 할 수 있고 실행 가능한 코드를 보는 것이 도움이됩니다. 전체 프로그램이 아니라 관련없는 많은 코드로 우리를 늪에 빠뜨릴만큼 커지지 않고 문제를 재현하기에 충분합니다. –

+1

GridBagComponents의 weightx 또는 weighty 필드를 설정할 위치가 어디에도 표시되지 않습니다. 1.0으로 설정하면 어떻게됩니까? 이것이 도움이되지 않는다면 문제를 보여주는 컴파일 가능한 실행 가능한 소형 프로그램 인 [SSCCE] (http://sscce.org) 게시를 다시 고려하십시오. –

+0

내가 그렇게하면 아무 것도 바뀌지 않는 것 같습니다. 크기 조절 동작은 여전히 ​​발생합니다. – lilroo

답변

3

가 나는 부분과 같이 문제를 재현 할 수 있었다 :
enter image description hereenter image description here

그리고 탭을 클릭 한 후 :

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class Foo002 { 

    private static final int ROWS = 5; 

    private static void createAndShowGui() { 
     JPanel assignmentsPanel = new JPanel(new GridBagLayout()); 
     final JTextArea textarea = new JTextArea(ROWS, 20); 

     GridBagConstraints c = new GridBagConstraints(); 
     int insetGap = 2; 
     c.insets = new Insets(insetGap, insetGap, insetGap, insetGap); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridwidth = 1; 
     c.gridheight = 1; 
     c.weightx = 1.0; 
     c.weighty = 1.0; 
     String[] labels = { "title", "author", "date due", "date set", 
      "set weighting" }; 
     int row = 0; 
     for (int i = 0; i < labels.length; i++) { 
     JLabel label = new JLabel(labels[i], SwingConstants.CENTER); 
     c.gridx = 0; 
     c.gridy = i; 
     assignmentsPanel.add(label, c); 
     c.gridx = 1; 
     JTextField textfield = new JTextField(10); 
     assignmentsPanel.add(textfield, c); 

     label.setPreferredSize(textfield.getPreferredSize()); 
     row++; 
     } 
     c.gridx = 0; 
     c.gridy = row; 
     c.fill = GridBagConstraints.HORIZONTAL; 
     Action myAction = new AbstractAction("Fill Area") { 

     @Override 
     public void actionPerformed(ActionEvent arg0) { 
      StringBuilder sb = new StringBuilder(); 
      for (int i = 0; i < 10; i++) { 
       sb.append("foo bar bif baz spam\n"); 
      } 
      textarea.setText(sb.toString()); 
     } 
     }; 
     assignmentsPanel.add(new JButton(myAction), c); 
     c.gridx = 1; 
     assignmentsPanel.add(new JButton("Button 2"), c); 
     row++; 

     c.gridx = 0; 
     c.gridy = row; 
     c.gridwidth = 2; 
     c.gridheight = ROWS; 

     JScrollPane scrollpane = new JScrollPane(textarea); 

     assignmentsPanel.add(scrollpane, c); 

     JTabbedPane tabbedPanel = new JTabbedPane(); 
     tabbedPanel.add("Assignments", assignmentsPanel); 
     tabbedPanel.add("Modules", new JPanel()); 

     JOptionPane.showMessageDialog(null, tabbedPanel, "Foo", 
      JOptionPane.PLAIN_MESSAGE); 

    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGui(); 
     } 
     }); 
    } 
} 

을하기 전에 및 JTextArea에 텍스트를 추가 한 후,이 같은 모습
enter image description here

그러나 JScrollPan 개 수직 스크롤 :

실행이 보이는 경우
// JScrollPane scrollpane = new JScrollPane(textarea); 
    JScrollPane scrollpane = new JScrollPane(textarea, 
     JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
     JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 

:
enter image description here

도 맥 OS에 좋은 보이는 :

enter image description here

+0

방금 ​​시도했지만 문제가 여전히 발생합니다./고맙습니다. – lilroo

+4

@lilroo : 위에서 게시 한 것과 비슷한 [sscce] (http://sscce.org)를 게시하여 증명하십시오. 그러면 문제가 무엇인지 정확히 알 수 있습니다. –

1

당신은 구라 두 가방을 사용하여 절제를 시도 할 수 있습니다 및 중첩 된 JPanels를 대신 사용하십시오. 간단한 예제를 만들 자유를 얻었습니다.

public class LilrooPanel extends JPanel 
{ 
    private static final int GAP = 5; 

    public static void main(String[] args){ 
     JFrame main = new JFrame("Dims"); 
     JTabbedPane tabbed = new JTabbedPane(); 
     JPanel myPanel = new LilrooPanel(); 
     tabbed.add("Assignments", myPanel); 
     tabbed.add("Modules", new JPanel()); 
     main.setContentPane(tabbed); 
     main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     main.setSize(400, 400); 
     main.setLocationRelativeTo(null); 
     main.setVisible(true); 
    } 

    public LilrooPanel(){ 
     super(new BorderLayout(0, GAP)); 
     setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP)); 
     Box north = new Box(BoxLayout.Y_AXIS); 

     north.add(new BorderPanel("Assignment Title", new JTextField())); 
     north.add(Box.createRigidArea(new Dimension(0, GAP))); 
     north.add(new BorderPanel("Author", new JTextField())); 
     north.add(Box.createRigidArea(new Dimension(0, GAP))); 
     north.add(new BorderPanel("Date Due", new JTextField())); 
     north.add(Box.createRigidArea(new Dimension(0, GAP))); 
     north.add(new BorderPanel("Date Set", new JTextField())); 
     north.add(Box.createRigidArea(new Dimension(0, GAP))); 
     north.add(new BorderPanel("Set Weighting", new JTextField())); 
     north.add(Box.createRigidArea(new Dimension(0, GAP))); 

     JPanel buttonsPanel = new JPanel(); 
     buttonsPanel.add(new JButton("Add Assignment")); 
     buttonsPanel.add(new JButton("Remove Assignment")); 
     north.add(buttonsPanel); 

     add(north, BorderLayout.NORTH); 

     add(new JScrollPane(new JTable(new Object[][]{}, new Object[]{"ModTitle", "ModId", "Assignments"}))); 
    } 

    private static class BorderPanel extends JPanel 
    { 
     private static final Dimension LABELS_WIDTH = new Dimension(100, 0); 

     public BorderPanel(String label, JComponent right){ 
      super(new BorderLayout(GAP, 0)); 
      JLabel jLabel = new JLabel(label); 
      jLabel.setPreferredSize(LABELS_WIDTH); 
      add(jLabel, BorderLayout.WEST); 
      add(right); 
     } 
    } 
} 
관련 문제