问题
I want to add a vertical scroll-bar on my JFrame
with null
layout.
Is it possible or not? please help!
回答1:
Just set the JScrollPane
as ContentPane
for JFrame
as it is described here:
public class TabbedPaneTest {
public static void main(String [] a) {
final JFrame frame = new JFrame();
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane pane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
frame.setContentPane(pane);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
frame.setVisible(true);
}
});
}
}
回答2:
In eclipse IDE you can use following code
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
public class Test {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test window = new Test();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Test() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel container = new JPanel();
JScrollPane jsp = new JScrollPane(container);
container.setPreferredSize(new Dimension(500, 250));
container.setLayout(null);
JLabel lblHelloWorld = new JLabel("Hello World");
lblHelloWorld.setBounds(10, 11, 101, 14);
container.add(lblHelloWorld);
frame.getContentPane().add(jsp);
}
}
来源:https://stackoverflow.com/questions/18893369/how-to-add-scrollbar-in-jframe-with-null-layout