2013-11-21 3 views
0

저는 Swing에 다소 익숙하며 null 레이아웃으로 구성 요소를 배치하는 방법을 실험하고있었습니다. 버튼 b3 (끝 부분)이 패널 pan1의 반쪽으로 가고 반은 보이지 않는 프로그램입니다.null 레이아웃 패널 스크롤

pan1을 가져올 스크롤 패널을 추가했고 스크롤 패널이 거기에있는 것처럼 보이지만 스크롤하지 않습니다. 전체 버튼을 볼 수 있도록 스크롤 할 수있는 방법이 있습니까?

다른 구성 요소의 절대 위치 (null 레이아웃)를 변경하고 싶지 않습니다. 버튼 b3이 완전히 보이도록 스크롤하고 싶습니다.

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
public class Swing18 
{ 
public static void main() 
{ 


JFrame frame1 = new JFrame("TESTING"); 

frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
frame1.setSize(1000,700); 
frame1.setLocation(200,100); 
frame1.setResizable(false); 


frame1.setLayout(null); 


JPanel pan1 = new JPanel(); 
pan1.setBackground(Color.green); 
pan1.setBounds(0,0,900,600); 
frame1.add(pan1); 


pan1.setLayout(null); 

JButton east = new JButton("East"); 
JButton west = new JButton("West"); 
JButton north = new JButton("North"); 
JButton south = new JButton("South"); 


Color cr1 = new Color(0,127,0); 
Font ft1 =new Font("impact",Font.BOLD,25); 



north.setForeground(Color.white); 
north.setBackground(cr1); 

south.setForeground(Color.white); 
south.setBackground(cr1); 

east.setForeground(Color.white); 
east.setBackground(Color.blue); 
east.setFont(ft1); 
east.setToolTipText(" This is the EAST zone"); 

west.setForeground(Color.white); 
west.setBackground(Color.blue); 
west.setFont(ft1); 
west.setToolTipText(" This is the WEST zone"); 




JLabel lb1 = new JLabel(" Label 1 "); 

JLabel lb2 = new JLabel(" Label 2 "); 
lb2.setOpaque(true); 
lb2.setForeground(Color.white); 
lb2.setBackground(Color.black); 
lb2.setFont(ft1); 




JTextField tf1 =new JTextField(" TextField1"); 
tf1.setForeground(Color.white); 
tf1.setBackground(Color.black); 
tf1.setFont(ft1); 


JTextField tf2 =new JTextField("TextField 2"); 




JTextArea ta1= new JTextArea("Enter TA",5,30); 
ta1.setForeground(Color.white); 
ta1.setBackground(Color.black); 
ta1.setFont(ft1); 
ta1.setLineWrap(true); 



JSlider sd1 = new JSlider(50,150); 
sd1.setMajorTickSpacing(20); 
sd1.setMinorTickSpacing(10); 
sd1.setPaintTicks(true);//essential for visible ticks 
sd1.setPaintLabels(true);//essential for visible numbers 
sd1.setForeground(Color.white); 
sd1.setBackground(Color.black); 

JSlider sd2 = new JSlider(JSlider.VERTICAL,50,200,100); 
sd2.setMajorTickSpacing(25); 
sd2.setMinorTickSpacing(5); 
sd2.setPaintTicks(true); 
sd2.setPaintLabels(true); 

JSlider sd3 = new JSlider(JSlider.HORIZONTAL,50,200,100); 
sd3.setMajorTickSpacing(25); 
sd3.setMinorTickSpacing(5); 



String[] fruit = {"Apple","Banana"," Coconut"," Date","Jujube","Guava","Grapes","Mango","Orange","Water Melon","Very Long Named Fruit"}; 
JList lt1 = new JList(fruit); 
lt1.setForeground(Color.white); 
lt1.setBackground(Color.black); 


String[] country = {"Armenia","Bangladesh","China","Denmark","Egypt","France","Germany","Holland","India","Japan","Kyrgystan"," Lithuania","Mexico","North Korea","Singapore","Uruguay","Vanuatu"/*,"Very Big Named Country"*/}; 
JComboBox cb1 = new JComboBox(country); 
cb1.setForeground(Color.yellow); 
cb1.setBackground(Color.red); 
cb1.setFont(ft1); 




ButtonGroup bg1= new ButtonGroup(); 

JRadioButton rb1 = new JRadioButton(" Rose "); 
rb1.setForeground(Color.cyan); 
rb1.setBackground(cr1); 
bg1.add(rb1); 

JRadioButton rb2 = new JRadioButton(" Lily "); 
rb2.setForeground(Color.cyan); 
rb2.setBackground(cr1); 
bg1.add(rb2); 

JRadioButton rb3 = new JRadioButton(" Tulip "); 
rb3.setForeground(Color.cyan); 
rb3.setBackground(cr1); 
bg1.add(rb3); 

JRadioButton rb4 = new JRadioButton(" SunFlower "); 
rb4.setForeground(Color.cyan); 
rb4.setBackground(cr1); 
bg1.add(rb4); 



JCheckBox ch1 = new JCheckBox(" See "); 
ch1.setForeground(Color.magenta); 
ch1.setBackground(Color.cyan); 
bg1.add(ch1); 


JCheckBox ch2 = new JCheckBox(" Touch "); 
ch2.setForeground(Color.magenta); 
ch2.setBackground(Color.cyan); 
bg1.add(ch2); 


JCheckBox ch3 = new JCheckBox(" Smell "); 
ch3.setForeground(Color.magenta); 
ch3.setBackground(Color.cyan); 
bg1.add(ch3); 



east.setBounds(400,200,80,100); 
pan1.add(east); 

west.setBounds(20,200,80,100); 
pan1.add(west); 

north.setBounds(200,10,100,80); 
pan1.add(north); 

south.setBounds(200,510,100,80); 
pan1.add(south); 



lb1.setBounds(0,0,100,50); 
pan1.add(lb1); 
lb2.setBounds(0,80,100,50); 
pan1.add(lb2); 


tf1.setBounds(10,350,80,30); 
pan1.add(tf1); 
tf2.setBounds(10,500,80,30); 
pan1.add(tf2); 



ta1.setBounds(400,10,100,180); 
pan1.add(ta1); 

sd1.setBounds(140,120,200,50); 
pan1.add(sd1); 
sd2.setBounds(210,200,50,200); 
pan1.add(sd2); 
sd3.setBounds(140,410,200,50); 
pan1.add(sd3); 


lt1.setBounds(520,20,120,200); 
pan1.add(lt1); 

cb1.setBounds(520,310,180,50); 
pan1.add(cb1); 


JScrollPane sp1 = new JScrollPane(lt1,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 
sp1.setBounds(lt1.getX(),lt1.getY(),lt1.getWidth(),lt1.getHeight()); 
pan1.add(sp1); 


JScrollPane sp2 = new JScrollPane(cb1,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 
sp2.setBounds(cb1.getX(),cb1.getY(),cb1.getWidth(),cb1.getHeight()); 
pan1.add(sp2); 


JScrollPane sp3 = new JScrollPane(ta1,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 
sp3.setBounds(ta1.getX(),ta1.getY(),ta1.getWidth()+10,ta1.getHeight()+10); 
pan1.add(sp3); 



JScrollPane sp4 = new JScrollPane(sd3,JScrollPane.VERTICAL_SCROLLBAR_NEVER,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 
sp4.setBounds(sd3.getX(),sd3.getY(),sd3.getWidth(),sd3.getHeight()); 
pan1.add(sp4); 


JScrollPane sp5 = new JScrollPane(tf1,JScrollPane.VERTICAL_SCROLLBAR_NEVER,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 
sp5.setBounds(tf1.getX(),tf1.getY(),tf1.getWidth()+20,tf1.getHeight()+20);//20,20 seems the ideal width and height to add 
pan1.add(sp5); 


//Now we try scrollpane on a panel 

JScrollPane sp6 = new JScrollPane(pan1,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 
sp6.setBounds(pan1.getX(),pan1.getY(),pan1.getWidth()+20,pan1.getHeight()+20); 
frame1.add(sp6); 




JPanel pan2 = new JPanel(); 
pan2.setBackground(Color.red); 
pan2.setBounds(680,10,120,250); 
pan1.add(pan2); 

pan2.setLayout(new BoxLayout(pan2,BoxLayout.Y_AXIS)); 

pan2.add(rb1); 
pan2.add(rb2); 
pan2.add(rb3); 
pan2.add(rb4); 

pan2.add(ch1); 
pan2.add(ch2); 
pan2.add(ch3); 






    **JButton b3 = new JButton("A Very Big Button");** 
    b3.setBackground(Color.white); 
    b3.setBounds(850,40,120,50); 
    pan1.add(b3); 




/* 

*/ 
frame1.setVisible(true); 

} 
} 

답변

3

난 그냥 그 버튼 (B3)는보기에 완전히 그래서 스크롤합니다.

null 레이아웃을 사용하는 경우 스크롤 . 그래서 null 레이아웃을 사용하지 않아야합니다.

스윙은 레이아웃 관리자와 함께 사용하도록 설계되었습니다. 레이아웃 관리자를 사용하면 스크롤이 자동으로 작동합니다.

+0

예, 정확하게! 1 + –

+0

답변 해 주셔서 감사합니다. 그러나 레이아웃 관리자로 절대 위치를 설정할 수 있습니까? 내가 그리드 레이아웃 나누기로 할 수있을 것 같아요. 그러나 그 레이아웃은 매우 융통성이 없어 보입니다. 격자 배치에서 구성 요소 배치에 여러 셀을 사용할 수 있습니까? – user3015246

+0

@ user3015246, 레이아웃 관리자를 사용하는 요점은 절대 위치를 지정하지 않기 때문입니다. 유연하지 않기 때문에 절대 위치를 지정할 필요가 없습니다. 각 레이아웃 관리자는 특정 요구에 맞게 설계되었습니다. 레이아웃 관리자를 결합하여 원하는 레이아웃을 얻을 수 있습니다. 단일 레이아웃 관리자를 사용하지 않아도됩니다. – camickr

3

다른 구성 요소의 절대 위치 (null 레이아웃)를 변경하고 싶지 않습니다.

이 제한 사항을 제거 할 것을 강력히 권장합니다. 그렇게하고 지능적인 방법으로 적절한 레이아웃 관리자를 사용하면 스크롤이 쉬워집니다.

그렇지 않으면 널 레이아웃을 절대적으로 사용해야하는 경우 (그리고 내가하는 것 같지 않은 경우) 스크롤 가능한 인터페이스를 구현하는 고유 한 JPanel 파생 클래스를 만드는 것이 좋습니다.하지만 훨씬 많은 작업이 필요하며 버그가 더 많이 발생합니다. . 여담으로 또한


, 그것은 String[] args 매개 변수가 누락으로 주요 방법은 위의 방법으로 문제가 해결되지 않습니다.

편집 2 : 컨테이너에 여러 번 구성 요소를 추가하는 것과 다른 이상한 일을하는 것을 포함하여 코드에 많은 문제가 있습니다. 스윙 튜토리얼을 들으셨습니까? 그렇지 않다면 끝까지 도움이 될 것입니다.



편집이 또한 참고를위한 JScrollPane의로 JPanel을 여기에, 뷰포트의보기로 개최 된 구성 요소가 해당 뷰보다 커야한다 "스크롤".

예를 들어, 기본 크기를 JScrollPane (kleopatra를 용서하십시오.)보다 크게 설정하여 주 JPanel을 "스크롤 가능"으로 만들 수 있습니다. 코드에는 코드를 많이 사용하지 않는 것이 좋습니다. 경계 값을 설정하려면 선호 크기는 두 가지이지만 권장 크기가 중요하다는 것을 나타 내기 위해 게시됩니다. 또한 JFrame의 null 레이아웃을 제거하고 표시하기 전에 JFrame을 포장했습니다.

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

public class Swing18 { 
    private static final Dimension PAN1_DIM = new Dimension(1000, 800); 
    private static final Dimension SP6_DIM = new Dimension(700, 500); 

    // **** note that the main method needs parameters!! **** 
    public static void main(String[] args) { 

     JFrame frame1 = new JFrame("TESTING"); 

     frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     // frame1.setSize(1000, 700); 
     // frame1.setLocation(200, 100); 
     // frame1.setResizable(false); 

     // frame1.setLayout(null); 

     JPanel pan1 = new JPanel(); 
     pan1.setBackground(Color.green); 
     // pan1.setBounds(0, 0, 900, 600); 
     pan1.setPreferredSize(PAN1_DIM); 
     // frame1.add(pan1); 

     pan1.setLayout(null); 

     JButton east = new JButton("East"); 
     JButton west = new JButton("West"); 
     JButton north = new JButton("North"); 
     JButton south = new JButton("South"); 

     Color cr1 = new Color(0, 127, 0); 
     Font ft1 = new Font("impact", Font.BOLD, 25); 

     north.setForeground(Color.white); 
     north.setBackground(cr1); 

     south.setForeground(Color.white); 
     south.setBackground(cr1); 

     east.setForeground(Color.white); 
     east.setBackground(Color.blue); 
     east.setFont(ft1); 
     east.setToolTipText(" This is the EAST zone"); 

     west.setForeground(Color.white); 
     west.setBackground(Color.blue); 
     west.setFont(ft1); 
     west.setToolTipText(" This is the WEST zone"); 

     JLabel lb1 = new JLabel(" Label 1 "); 

     JLabel lb2 = new JLabel(" Label 2 "); 
     lb2.setOpaque(true); 
     lb2.setForeground(Color.white); 
     lb2.setBackground(Color.black); 
     lb2.setFont(ft1); 

     JTextField tf1 = new JTextField(" TextField1"); 
     tf1.setForeground(Color.white); 
     tf1.setBackground(Color.black); 
     tf1.setFont(ft1); 

     JTextField tf2 = new JTextField("TextField 2"); 

     JTextArea ta1 = new JTextArea("Enter TA", 5, 30); 
     ta1.setForeground(Color.white); 
     ta1.setBackground(Color.black); 
     ta1.setFont(ft1); 
     ta1.setLineWrap(true); 

     JSlider sd1 = new JSlider(50, 150); 
     sd1.setMajorTickSpacing(20); 
     sd1.setMinorTickSpacing(10); 
     sd1.setPaintTicks(true);// essential for visible ticks 
     sd1.setPaintLabels(true);// essential for visible numbers 
     sd1.setForeground(Color.white); 
     sd1.setBackground(Color.black); 

     JSlider sd2 = new JSlider(JSlider.VERTICAL, 50, 200, 100); 
     sd2.setMajorTickSpacing(25); 
     sd2.setMinorTickSpacing(5); 
     sd2.setPaintTicks(true); 
     sd2.setPaintLabels(true); 

     JSlider sd3 = new JSlider(JSlider.HORIZONTAL, 50, 200, 100); 
     sd3.setMajorTickSpacing(25); 
     sd3.setMinorTickSpacing(5); 

     String[] fruit = { "Apple", "Banana", " Coconut", " Date", "Jujube", 
      "Guava", "Grapes", "Mango", "Orange", "Water Melon", 
      "Very Long Named Fruit" }; 
     JList lt1 = new JList(fruit); 
     lt1.setForeground(Color.white); 
     lt1.setBackground(Color.black); 

     String[] country = { "Armenia", "Bangladesh", "China", "Denmark", 
      "Egypt", "France", "Germany", "Holland", "India", "Japan", 
      "Kyrgystan", " Lithuania", "Mexico", "North Korea", "Singapore", 
      "Uruguay", "Vanuatu"/* ,"Very Big Named Country" */}; 
     JComboBox cb1 = new JComboBox(country); 
     cb1.setForeground(Color.yellow); 
     cb1.setBackground(Color.red); 
     cb1.setFont(ft1); 

     ButtonGroup bg1 = new ButtonGroup(); 

     JRadioButton rb1 = new JRadioButton(" Rose "); 
     rb1.setForeground(Color.cyan); 
     rb1.setBackground(cr1); 
     bg1.add(rb1); 

     JRadioButton rb2 = new JRadioButton(" Lily "); 
     rb2.setForeground(Color.cyan); 
     rb2.setBackground(cr1); 
     bg1.add(rb2); 

     JRadioButton rb3 = new JRadioButton(" Tulip "); 
     rb3.setForeground(Color.cyan); 
     rb3.setBackground(cr1); 
     bg1.add(rb3); 

     JRadioButton rb4 = new JRadioButton(" SunFlower "); 
     rb4.setForeground(Color.cyan); 
     rb4.setBackground(cr1); 
     bg1.add(rb4); 

     JCheckBox ch1 = new JCheckBox(" See "); 
     ch1.setForeground(Color.magenta); 
     ch1.setBackground(Color.cyan); 
     bg1.add(ch1); 

     JCheckBox ch2 = new JCheckBox(" Touch "); 
     ch2.setForeground(Color.magenta); 
     ch2.setBackground(Color.cyan); 
     bg1.add(ch2); 

     JCheckBox ch3 = new JCheckBox(" Smell "); 
     ch3.setForeground(Color.magenta); 
     ch3.setBackground(Color.cyan); 
     bg1.add(ch3); 

     east.setBounds(400, 200, 80, 100); 
     pan1.add(east); 

     west.setBounds(20, 200, 80, 100); 
     pan1.add(west); 

     north.setBounds(200, 10, 100, 80); 
     pan1.add(north); 

     south.setBounds(200, 510, 100, 80); 
     pan1.add(south); 

     lb1.setBounds(0, 0, 100, 50); 
     pan1.add(lb1); 
     lb2.setBounds(0, 80, 100, 50); 
     pan1.add(lb2); 

     tf1.setBounds(10, 350, 80, 30); 
     pan1.add(tf1); 
     tf2.setBounds(10, 500, 80, 30); 
     pan1.add(tf2); 

     ta1.setBounds(400, 10, 100, 180); 
     pan1.add(ta1); 

     sd1.setBounds(140, 120, 200, 50); 
     pan1.add(sd1); 
     sd2.setBounds(210, 200, 50, 200); 
     pan1.add(sd2); 
     sd3.setBounds(140, 410, 200, 50); 
     pan1.add(sd3); 

     lt1.setBounds(520, 20, 120, 200); 
     pan1.add(lt1); 

     cb1.setBounds(520, 310, 180, 50); 
     pan1.add(cb1); 

     JScrollPane sp1 = new JScrollPane(lt1, 
      JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
      JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 
     sp1.setBounds(lt1.getX(), lt1.getY(), lt1.getWidth(), lt1.getHeight()); 
     pan1.add(sp1); 

     JScrollPane sp2 = new JScrollPane(cb1, 
      JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
      JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 
     sp2.setBounds(cb1.getX(), cb1.getY(), cb1.getWidth(), cb1.getHeight()); 
     pan1.add(sp2); 

     JScrollPane sp3 = new JScrollPane(ta1, 
      JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
      JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 
     sp3.setBounds(ta1.getX(), ta1.getY(), ta1.getWidth() + 10, 
      ta1.getHeight() + 10); 
     pan1.add(sp3); 

     JScrollPane sp4 = new JScrollPane(sd3, 
      JScrollPane.VERTICAL_SCROLLBAR_NEVER, 
      JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 
     sp4.setBounds(sd3.getX(), sd3.getY(), sd3.getWidth(), sd3.getHeight()); 
     pan1.add(sp4); 

     JScrollPane sp5 = new JScrollPane(tf1, 
      JScrollPane.VERTICAL_SCROLLBAR_NEVER, 
      JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 
     sp5.setBounds(tf1.getX(), tf1.getY(), tf1.getWidth() + 20, 
      tf1.getHeight() + 20);// 20,20 seems the ideal width and height to 
            // add 
     pan1.add(sp5); 

     // Now we try scrollpane on a panel 

     JScrollPane sp6 = new JScrollPane(pan1, 
      JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
      JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 
     // sp6.setBounds(pan1.getX(), pan1.getY(), pan1.getWidth() + 20, 
     // pan1.getHeight() + 20); 

     sp6.setPreferredSize(SP6_DIM); 
     frame1.add(sp6); 

     JPanel pan2 = new JPanel(); 
     pan2.setBackground(Color.red); 
     pan2.setBounds(680, 10, 120, 250); 
     pan1.add(pan2); 

     pan2.setLayout(new BoxLayout(pan2, BoxLayout.Y_AXIS)); 

     pan2.add(rb1); 
     pan2.add(rb2); 
     pan2.add(rb3); 
     pan2.add(rb4); 

     pan2.add(ch1); 
     pan2.add(ch2); 
     pan2.add(ch3); 

     JButton b3 = new JButton("A Very Big Button"); // !! 
     b3.setBackground(Color.white); 
     b3.setBounds(850, 40, 120, 50); 
     pan1.add(b3); 

     frame1.pack(); 
     frame1.setLocationRelativeTo(null); 
     frame1.setVisible(true); 

    } 
} 
+0

답변 해 주셔서 감사합니다. 사실, 약간의 실험을하고 싶었 기 때문에 의도적으로 null 레이아웃을 사용했습니다. 나는 쉬운 길이 있기를 바랐다. – user3015246

+0

@ user3015246 : 코드가 가장 이상하다고 말하는 것은 아주 이상합니다. 자습서를 읽으십시오. ** 많이 ** 얻을 수 있습니다. –

+0

자습서 중 어떤 것이 있습니까? 왜 이상하지? – user3015246