2013-11-21 168 views
0

我对Swing有点新,我正在试验如何将组件放在空布局中。 这是一个程序,其中按钮b3(朝向末端)在面板pan1内部变为一半,而一半不可见。滚动空布局面板

我添加了一个滚动窗格来取pan1,并且滚动窗格似乎在那里,但它不滚动。有没有办法滚动,以便我可以查看完整的按钮?

我不想更改其他组件的绝对位置(空布局)。我只想滚动以便按钮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是完全视图。

当您使用空布局时滚动doesn't work。这就是为什么你不应该使用空布局。

Swing设计用于布局管理器。当您使用布局管理器时,滚动将自动运行。

+0

是的,正好! 1+ –

+0

感谢您的回答。但是,我可以用任何布局管理器设置绝对位置吗?我想我可以通过分割来完成网格布局。但是这种布局看起来非常不灵活。我可以使用多个单元格在网格布局中放置组件吗? – user3015246

+0

@ user3015246,使用布局管理器的目的是让你'不'指定绝对位置。没有必要指定绝对位置,因为它不灵活。每个布局管理器都是为特定需求而设计的你可以混合和匹配布局管理器来获得你想要的布局。您不必强制使用单个布局管理器。 – camickr

3

我不想更改其他组件的绝对位置(空布局)。

我强烈要求你摆脱这个限制。这样做并以智能的方式使用合适的布局管理器,并且滚动将变得简单。

否则,如果你绝对必须使用null布局(我怀疑你是这样做的),可以考虑创建自己创建的实现Scrollable接口的JPanel派生类,但这将会有更多的工作,并且通常会有更多的错误。


而且顺便说一句,注意,因为它缺少String[] args参数main方法上面将无法工作。编辑2:你的代码有更多的错误,包括多次添加组件到容器,以及其他奇怪的事件。你经历过Swing教程吗?如果没有,请检查他们,因为他们会帮助你没有结束。


编辑2
另请注意,JScrollPane的“滚动”举行,其视的观点,在这里你的JPanel组件,具有比视图大。例如,我可以通过将您的主JPanel设置为比JScrollPane更大的首选大小(请原谅我kleopatra)来使您的主JPanel“可滚动”。请注意,您的代码中有很多我不会推荐的设置,并且设置边界首选大小是其中的两个,但是这是张贴以表明首选大小很重要。我也摆脱了JFrame的空白布局,并在显示之前打包了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

感谢您的回答。实际上,我故意使用了空白布局,因为我想对它进行一些尝试。我希望能有一些简单的方法。 – user3015246

+0

@ user3015246:你的代码至少可以说是超级怪异的。请阅读教程,因为您将获得**很多**。 –

+0

哪些教程?为什么这很奇怪? – user3015246