JTabbedPane
我需要将最小化和关闭按钮放在我结束的右上角。为清楚起见,圈出的区域是我需要的两个按钮(我有一个未装饰的框架)
谁能指导我如何实现这一目标?我的问题是(更多)关于按钮的定位(在 JTabbedPane 上)而不是实现最小化/关闭的功能
谢谢!
JTabbedPane
我需要将最小化和关闭按钮放在我结束的右上角。为清楚起见,圈出的区域是我需要的两个按钮(我有一个未装饰的框架)
谁能指导我如何实现这一目标?我的问题是(更多)关于按钮的定位(在 JTabbedPane 上)而不是实现最小化/关闭的功能
谢谢!
作为子项添加到 JTabbedPane 的所有组件都将包装在选项卡中。这意味着无法将按钮直接添加到 JTabbedPane。如果您想将关闭和最小化按钮直接作为选项卡式窗格的一部分(而不是稍微放在一边),我建议使用自定义选项卡组件 ( JTabbedPane.setTabComponentAt()
) 将两个选项卡添加到选项卡式窗格中,这些组件是按钮。由于按钮是选项卡的唯一组件,因此无法选择实际的“选项卡”本身,只会发生按钮事件。使用 JTabbedPane 无法固定选项卡的位置,这将导致这两个“按钮选项卡”在您选择的布局中浮动。在它们的左侧插入更多选项卡将具有始终以关闭和最小化按钮结束选项卡列表的效果。
我有一个解决这个问题的方法:只需在选项卡式窗格旁边放一个工具栏
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.JTabbedPane;
import java.awt.FlowLayout;
import java.awt.Insets;
import javax.swing.JToolBar;
import java.awt.ComponentOrientation;
import javax.swing.JButton;
import java.awt.Cursor;
public class PanelTab extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (InstantiationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (UnsupportedLookAndFeelException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
PanelTab frame = new PanelTab();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public PanelTab() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[] { 424, 0 };
gbl_contentPane.rowHeights = new int[] { 51, 0 };
gbl_contentPane.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
gbl_contentPane.rowWeights = new double[] { 1.0, Double.MIN_VALUE };
contentPane.setLayout(gbl_contentPane);
JPanel panel = new JPanel();
GridBagConstraints gbc_panel = new GridBagConstraints();
gbc_panel.fill = GridBagConstraints.BOTH;
gbc_panel.gridx = 0;
gbc_panel.gridy = 0;
contentPane.add(panel, gbc_panel);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[] { 184, 0 };
gbl_panel.rowHeights = new int[] { 54, 0 };
gbl_panel.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
gbl_panel.rowWeights = new double[] { 1.0, Double.MIN_VALUE };
panel.setLayout(gbl_panel);
JToolBar toolBar = new JToolBar();
toolBar.setOpaque(false);
toolBar.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
toolBar.setFloatable(false); // The toolbar must not be float
GridBagConstraints gbc_toolBar = new GridBagConstraints();
gbc_toolBar.anchor = GridBagConstraints.NORTHEAST;
gbc_toolBar.gridx = 0;
gbc_toolBar.gridy = 0;
panel.add(toolBar, gbc_toolBar);
JButton btnHola = new JButton("You must put your exit icon here");
btnHola.setOpaque(false);
toolBar.add(btnHola);
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
GridBagConstraints gbc_tabbedPane = new GridBagConstraints();
gbc_tabbedPane.fill = GridBagConstraints.BOTH;
gbc_tabbedPane.gridx = 0;
gbc_tabbedPane.gridy = 0;
panel.add(tabbedPane, gbc_tabbedPane);
JPanel panel_1 = new JPanel();
tabbedPane.addTab("New tab", null, panel_1, null);
}
}
我想这可能会帮助你...