Java applet scrollbar
[Thanks for the answers. This comes for you http://www.youtube.com/watch?v=Vo0Cazxj_yc ] This might and should be a very easy question, but i could not find a solution.
I have a java applet, and i want a vertical scrollbar so that i can load thousands of buttons into the applet and use the scrollbar to see buttons down on the applet
.Buttons are used to select items. if button is pressed, the item is selected.
When i load buttons, all of them are shown on one screen, squeezed together to fit the screen in width and height (~1000px,~1000px). Below code is a portion of my program. Please comment.
JFrame frame = new JFrame();
NameClassifier nameClassifier = new NameClassifier();
JScrollPane scrollPane = new JScrollPane(nameClassifier);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VE开发者_高级运维RTICAL_SCROLLBAR_ALWAYS);
frame.add(scrollPane);
frame.getContentPane().add(nameClassifier);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
System.out.println("exiting");
import java.awt.*;
import javax.swing.*;
class ManyButtons {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
NameClassifier nameClassifier = new NameClassifier();
JScrollPane scrollPane = new JScrollPane(nameClassifier);
scrollPane.setVerticalScrollBarPolicy(
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
frame.add(scrollPane);
// nameClassifier has already been added to the scroll pane.
//frame.getContentPane().add(nameClassifier);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
System.out.println("exiting");
}
});
}
}
class NameClassifier extends JPanel {
NameClassifier() {
super(new GridLayout(0,10,2,2));
for (int ii=1; ii<=1000; ii++) {
add(new JButton("Button " + ii));
}
}
}
I think you want to use a Wrap Layout.
Don't add anything directly to the frame, so
frame.add(scrollPane);
is wrong.
Add things to the content pane. probably
scrollPane.add(nameClassifier);
frame.getContentPane().add(scrollPane);
btw, that is one pretty gui design. :)
精彩评论