0
在我的代碼中,用戶可以使用菜單將新按鈕添加到主JPanel。 JPanel使用FlowLayout表單,以便根據窗口的大小,更改每行上的數字或按鈕以適應最佳視圖...好的,在我引入JScrollPane之前就是如此。現在我有兩個問題:如何創建一個動態大小的JScrollPane w/JPanel作爲客戶端?
只要用戶將窗口重新調整到首選大小或更低,每行上的按鈕數就會停止更改。 *以前沒有這個問題,即使設置了首選大小。
我只能滾動以查看是JPanel的優選尺寸範圍內的按鈕。任何放在外面的東西都是不可查看的,並且不會增加JScrollPane範圍的大小。
下面的代碼:
package testit;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;
import javax.swing.border.LineBorder;
class TestIt extends JFrame implements ActionListener {
//Main window frame, content panel and scroll pane
private JFrame main_frame;
private JPanel main_panel;
private JScrollPane main_scroll;
//Screen size
private Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
public TestIt() {
//Set up the main frame
main_frame = new JFrame("Test Program");
main_frame.setLayout(new BorderLayout());
main_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main_frame.setIconImage(
new ImageIcon("src/testit/resources/img/app_icon.gif").getImage());
//Set up the content panel and add it to the scroll pane
main_panel = new JPanel();
main_panel.setLayout(new FlowLayout());
main_panel.setPreferredSize(
new Dimension(screen.width/10 * 6, screen.height/10 * 6));
main_scroll = new JScrollPane(main_panel);
//Add the menu bar and the scroll pane to the main frame
main_frame.add(main_scroll, BorderLayout.CENTER);
main_frame.setJMenuBar(createMenuBar());
//Display the main GUI
main_frame.pack();
main_frame.setLocationRelativeTo(null);
main_frame.setVisible(true);
}
private JMenuBar createMenuBar() {
//Create the top menu bar
JMenuBar top_menu_bar = new JMenuBar();
//Create the menu
JMenu main_menu = new JMenu ("Menu");
main_menu.setMnemonic(KeyEvent.VK_M);
top_menu_bar.add(main_menu);
//Create the menu items and add them to the menu
JMenuItem menu_item;
menu_item = new JMenuItem("Add New");
menu_item.setMnemonic(KeyEvent.VK_N);
menu_item.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.ALT_MASK));
menu_item.setActionCommand("new");
menu_item.addActionListener(this);
main_menu.add(menu_item);
menu_item = new JMenuItem("Save");
menu_item.setMnemonic(KeyEvent.VK_S);
menu_item.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.ALT_MASK));
menu_item.setActionCommand("save");
menu_item.addActionListener(this);
main_menu.add(menu_item);
menu_item = new JMenuItem("Exit");
menu_item.setMnemonic(KeyEvent.VK_X);
menu_item.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.ALT_MASK));
menu_item.setActionCommand("exit");
menu_item.addActionListener(this);
main_menu.add(menu_item);
//Return the assembled menu bar
return top_menu_bar;
}
@Override
public void actionPerformed(ActionEvent e) {
switch (e.getActionCommand()) {
case "new":
createThumb();
break;
case "save":
break;
default:
quit();
break;
}
}
private void createThumb() {
//Width and height for the thumbnail button
final int H = 170;
final int W = 150;
//Set up the thumbnail button
ImageIcon image = new ImageIcon("src/testit/resources/img/test.gif");
JButton thumb = new JButton(image);
thumb.setMargin(new Insets(0, 0, 0, 0));
thumb.setBorder(new LineBorder(Color.BLACK));
thumb.setBackground(Color.BLACK);
thumb.setPreferredSize(new Dimension(W, H));
thumb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
openNewFrame();
}
});
main_panel.add(thumb);
main_panel.validate();
}
private void openNewFrame() {
JFrame new_window = new JFrame();
new_window.setPreferredSize(new Dimension(400, 800));
new_window.pack();
new_window.setLocationRelativeTo(null);
new_window.setVisible(true);
}
private void quit() {
System.exit(0);
}
//Create an instance of the program
private static void runIt() {
TestIt program = new TestIt();
}
public static void main(String [] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
runIt();
}
});
}
}
我試過很多東西,我經歷過的Javadoc挖....我讀過很多東西和教程,但還是可以我的滾動/重新調整大小工作。
我喜歡它!但是,一個問題是,當我增加窗口的寬度時,它會增加按鈕的寬度。這是由於GridLayout vs FlowLayout的性質嗎?任何方式來防止GridLayout拉伸拇指?我猜設置可調整大小爲false或強制最大寬度可以做到這一點......但我不知道是否有其他方法? – 2013-05-12 03:15:45