gpt4 book ai didi

java - JTabBar 中的 JPanel 不可滚动

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:26:10 25 4
gpt4 key购买 nike

在此处输入代码我遇到了以下问题。

我正在向一个面板添加两个 JPanel,最后将其添加到我的 TabbedPane。但是,我想让它可以滚动,因此我只是让面板可以滚动,这只是在栏的中间添加了我的滚动条。

这是我的例子:

public class Test extends JFrame {

private static final long serialVersionUID = -4682396888922360841L;
private JMenuBar menuBar;
private JMenu mAbout;
private JMenu mMain;
private JTabbedPane tabbedPane;
public SettingsTab settings = new SettingsTab();

private void addMenuBar() {
menuBar = new JMenuBar();
mMain = new JMenu("Main");
mAbout = new JMenu("About");
menuBar.add(mMain);
menuBar.add(mAbout);
setJMenuBar(menuBar);
}

public void createTabBar() {
tabbedPane = new JTabbedPane(JTabbedPane.TOP);
JComponent panel2 = new JPanel();
tabbedPane.addTab("Test1", null, panel2,
"Displays the results");
tabbedPane.addTab("Settings", settings.createLayout());
add(tabbedPane);
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
}

private void makeLayout() {
setTitle("Test");
setLayout(new BorderLayout());
setPreferredSize(new Dimension(1000, 500));
addMenuBar();
createTabBar();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}

public void start() {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
makeLayout();
}
});
}

public static void main(String[] args) {
Test gui = new Test();
gui.start();
}

public class SettingsTab extends JPanel {
public JPanel createLayout() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(table1());
panel.add(Box.createRigidArea(new Dimension(0,10)));
panel.add(table2());
panel.add(Box.createRigidArea(new Dimension(0,10)));
panel.add(new JScrollBar());
return panel;
}

public JPanel table1() {
JPanel panel1 = new JPanel();
String[] columnNames = {"First Name", "Last Name"};
Object[][] data = {{"Kathy", "Smith"}, {"John", "Doe"},
{"Sue", "Black"}, {"Jane", "White"}, {"Joe", "Brown"}
};
final JTable table = new JTable(data, columnNames);
panel1.add(table);
panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
return panel1;
}

public JPanel table2() {
JPanel panel1 = new JPanel();
String[] columnNames = {"First Name", "Last Name"};
Object[][] data = { {"Kathy", "Smith"}, {"John", "Doe"},
{"Sue", "Black"}, {"Jane", "White"}, {"Joe", "Brown"}
};
final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
panel1.add(table);
panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
return panel1;
}
}
}

对我做错了什么有什么建议吗?

感谢您的回复!

更新

我用 JScrollPane 试过了,但还是不行:

public class Test extends JFrame {

private static final long serialVersionUID = -4682396888922360841L;

private JMenuBar menuBar;

private JMenu mAbout;

private JMenu mMain;

private JTabbedPane tabbedPane;

public SettingsTab settings = new SettingsTab();

private void addMenuBar() {

menuBar = new JMenuBar();

mMain = new JMenu("Main");
mAbout = new JMenu("About");

menuBar.add(mMain);
menuBar.add(mAbout);

setJMenuBar(menuBar);

}

public void createTabBar() {

tabbedPane = new JTabbedPane(JTabbedPane.TOP);

JComponent panel2 = new JPanel();
tabbedPane.addTab("Test1", null, panel2,
"Displays the results");

tabbedPane.addTab("Settings", settings.createLayout());

add(tabbedPane);

tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);

}

private void makeLayout() {

setTitle("Test");
setLayout(new BorderLayout());
setPreferredSize(new Dimension(1000, 500));

addMenuBar();
createTabBar();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);

}


public void start() {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
makeLayout();
}
});
}

public static void main(String[] args) {

Test gui = new Test();
gui.start();

}

public class SettingsTab extends JScrollPane {


public JPanel createLayout() {
JPanel panel = new JPanel();

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

panel.add(table1());
panel.add(Box.createRigidArea(new Dimension(0,10)));
panel.add(table2());
panel.add(Box.createRigidArea(new Dimension(0,10)));
panel.add(table3());
panel.add(Box.createRigidArea(new Dimension(0,10)));

add(new JScrollPane());

return panel;
}

public JPanel table1() {
JPanel panel1 = new JPanel();

String[] columnNames = {"First Name",
"Last Name"};

Object[][] data = {
{"Kathy", "Smith"},
{"John", "Doe"},
{"Sue", "Black"},
{"Jane", "White"},
{"Joe", "Brown"},
{"John", "Doe"},
{"Sue", "Black"},
{"Jane", "White"},
{"Joe", "Brown"}
};

final JTable table = new JTable(data, columnNames);

panel1.add(table);

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

return panel1;
}

public JPanel table2() {
JPanel panel1 = new JPanel();

String[] columnNames = {"First Name",
"Last Name"};

Object[][] data = {
{"Kathy", "Smith"},
{"John", "Doe"},
{"Sue", "Black"},
{"Jane", "White"},
{"Joe", "Brown"},
{"John", "Doe"},
{"Sue", "Black"},
{"Jane", "White"},
{"Joe", "Brown"}
};

final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);

panel1.add(table);

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

return panel1;
}

public JPanel table3() {
JPanel panel1 = new JPanel();

String[] columnNames = {"First Name",
"Last Name"};

Object[][] data = {
{"Kathy", "Smith"},
{"John", "Doe"},
{"Sue", "Black"},
{"Jane", "White"},
{"Joe", "Brown"},
{"John", "Doe"},
{"Sue", "Black"},
{"Jane", "White"},
{"Joe", "Brown"}
};

final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);

panel1.add(table);

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

return panel1;
}
}
}

最佳答案

必须在上面加一个JScrollPane! :)

我真的不知道你在那里做了什么,但基本上是这样的:

JPanel myPanel = new JPanel(); // the main content is on the myPanel
myPanel.add(myContent);// add everything you want

new JFrame().add(new JScrollPane(myPanel));

关于java - JTabBar 中的 JPanel 不可滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24904728/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com