2016-07-03 10 views
0

JScrollPane을 2 개의 목록에 사용하고 버튼을 전환하는 간단한 애플리케이션이 있습니다. 더 많은 스윙 요소를 추가하고 싶지만 object.setBounds으로 이동할 수는 없습니다. 이 메소드에서 작성하는 요소는 장소와 크기를 변경하지 않습니다.JScrollPane이 사용되어 다른 JComponents의 경계를 설정할 수 없습니다.

package paka; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.*;; 
public class Question extends JFrame { 
    private JList leftlist,rightlist; 
    private JButton movebutton; 
    private JLabel pointlessLabel; 
    private static String[] foods={"bacon","wings","ham","beef","more bacon"}; 

    public Question(){ 
     super("title"); 
     setLayout(new FlowLayout()); 

     leftlist=new JList(foods); 
     leftlist.setVisibleRowCount(3); 
     leftlist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); 
     add(new JScrollPane(leftlist)); 
     movebutton = new JButton("Move"); 

     movebutton.addActionListener(
       new ActionListener() { 

        @Override 
        public void actionPerformed(ActionEvent arg0) { 
         rightlist.setListData(leftlist.getSelectedValues()); 

        } 
       } 
       ); 
     add(movebutton); 

     rightlist = new JList(); 
     rightlist.setVisibleRowCount(3); 
     rightlist.setFixedCellWidth(100);rightlist.setFixedCellHeight(15); 
     rightlist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); 
     add(new JScrollPane(rightlist));   

     //Okay, deleting everything below we have only two list with button that moves elements from 1st list to 2nd 

     movebutton = new JButton("Click me!"); 
     movebutton.setBounds(700, 100, 80, 20); 
     add(movebutton); 

     pointlessLabel = new JLabel("I'm unbreakable"); 
     pointlessLabel.setBounds(500,200,100,50); 
     add(pointlessLabel); 



    } 

    public static void main(String args[]){ 
     Question go = new Question(); 
     go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     go.setSize(300,200); 
     go.setVisible(true); 
    } 
} 
+5

' 그래서 나는 다른 JElements의 경계를 정확히 정할 수 없다. 당신은 setBounds()를 사용해서는 안됩니다. Swing은 레이아웃 관리자와 함께 사용하도록 설계되었습니다. 레이아웃 관리자를 사용하면 스크롤 창이 자동으로 작동합니다. – camickr

+3

Java GUI는 다른 로케일의 다른 PLAF를 사용하여 다른 OS ', 화면 크기, 화면 해상도 등에서 작동해야합니다. 따라서, 픽셀 완벽한 레이아웃에 도움이되지 않습니다. 대신 레이아웃 관리자 또는 [조합] (http://stackoverflow.com/a/5630271/418556)과 [공백] 레이아웃 채우기 및 테두리 (http://stackoverflow.com/a/17874718/)를 사용하십시오. 418556). –

답변

4

당신은 (위의 그의 의견에 @AndrewThompson에 의해 명시된) 당신은 null layout로이 같은 결과를 달성하기 위해 Layout Managers의 조합을 사용합니다.

null layout의 사용은 다음을 참조하십시오 Null layout is evilWhy is it frowned upon to use a null layout in Swing?

을 코드로 당신은 당신이 setBounds() 방법이 있었다 동일한 출력을하실 수 있습니다. 내가 null layout을 사용하는 것을 멈추고 동일한 출력을 갖는 방법을 보여주기 때문에이 코드에 ActionListener을 추가하지 않았습니다. 예! 두 번째 것은 더 짧아 보이는데, 이는 pack()이기 때문입니다. 특정 창 크기가 필요하거나 필요하면 여전히 setSize()이 될 수 있습니다.

, 나는이이 문제를 해결하는 데 도움이되기를 바랍니다 아래에 더 많은 요소가 단지 더 JPanel의를 추가하고 pane에 추가 추가하지 않을 경우, 여전히, 우리는 복사 - 붙여 넣기를 할 수있는 Minimal Complete and Verifiable Example를 게시는 짧게 만들지 만, 제발 문제를 보여줍니다 및 권고 위에 다음과

중요 참고 :

내가 할 prototypeCellValue을 사용했기 때문에 나는 @MadProgrammer의 This answer's comment에서 견적을 가져올거야 rightList의 폭 짧은 :

값이 데이터의 예상 길이와 일치하지 않는

나는 또한이 표시 될 때, 단지주의 할 필요가 데이터를자를 수, 치는 prototypeCellValue 조심해야 할 것

enter image description here

import java.awt.FlowLayout; 

import javax.swing.BoxLayout; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JList; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.SwingUtilities; 

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

    public static void createAndShowGUI() { 
     String[] foods = { "bacon", "wings", "ham", "beef", "more bacon" }; 

     JFrame frame; 
     JPanel topPane, bottomPane, pane; 
     JList leftList, rightList; 
     JButton moveButton, clicMeButton; 
     JScrollPane scroll; 
     JLabel label; 

     frame = new JFrame("title"); 
     topPane = new JPanel(); 
     bottomPane = new JPanel(); 
     pane = new JPanel(); 
     leftList = new JList(foods); 
     rightList = new JList(); 
     moveButton = new JButton("Move"); 
     clicMeButton = new JButton("Click me!"); 
     label = new JLabel("I'm unbreakable"); 

     leftList.setVisibleRowCount(3); 
     rightList.setVisibleRowCount(3); 
     leftList.setPrototypeCellValue(String.format("%30s", "")); 
     rightList.setPrototypeCellValue(String.format("%30s", "")); 

     pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS)); 
     topPane.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); 

     scroll = new JScrollPane(leftList); 

     topPane.add(scroll); 
     topPane.add(moveButton); 

     scroll = new JScrollPane(rightList); 
     topPane.add(scroll); 

     bottomPane.setLayout(new FlowLayout()); 
     bottomPane.add(clicMeButton); 
     bottomPane.add(label); 

     pane.add(topPane); 
     pane.add(bottomPane); 
     frame.add(pane); 

     frame.pack(); 
     frame.setVisible(true); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 
}