JLabel 就像一个标签
JLabel like a tab
我正在尝试将 JLabel
用作 JTabbedPane
的选项卡,但我不知道该怎么做。可能吗?
如果没有,我正在尝试制作如下所示的标签:
Win10 选项卡:
how to align the text(not the tabs) to the left?
icon/text 的中心对齐由 LAF 控制。我从来没有发现覆盖默认 LAF 行为是一件容易的事,因为您需要为所有平台创建自定义 LAF。
另一种选择是确定用作选项卡组件的每个标签的首选大小,然后将所有这些标签的首选大小设置为相同的宽度。这将强制左对齐。
import javax.swing.*;
import java.awt.*;
public class TabbedPaneLeft extends JPanel
{
private JTabbedPane tabbedPane;
public TabbedPaneLeft()
{
ImageIcon icon = new ImageIcon( "copy16.gif" );
tabbedPane = new JTabbedPane();
tabbedPane.setTabPlacement(JTabbedPane.LEFT);
add( tabbedPane );
initTabComponent(icon, "Tab 1");
initTabComponent(icon, "Tabbed Pane 2");
initTabComponent(icon, "Tab 3");
adjustTabComponentSize();
}
private void initTabComponent(Icon icon, String text)
{
JLabel label = new JLabel( text );
label.setPreferredSize( new Dimension(300, 300) );
tabbedPane.addTab(null, null, label);
JLabel tabLabel = new JLabel( text );
tabLabel.setIcon( icon );
//tabLabel.setHorizontalAlignment(JLabel.LEFT); // doesn't work
//tabLabel.setAlignmentX(0.0f); // doesn't work
tabbedPane.setTabComponentAt(tabbedPane.getTabCount() - 1, tabLabel);
}
private void adjustTabComponentSize()
{
int width = 0;
// Find the width of the larget tab
for (int i = 0; i < tabbedPane.getTabCount(); i++)
{
Dimension d = tabbedPane.getTabComponentAt(i).getPreferredSize();
width = Math.max(width, d.width);
}
// Set the width of all tabs to match the largest
for (int i = 0; i < tabbedPane.getTabCount(); i++)
{
Component tabComponent = tabbedPane.getTabComponentAt(i);
Dimension d = tabComponent.getPreferredSize();
d.width = width;
tabComponent.setPreferredSize( d );
}
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TabbedPaneLeft());
frame.pack();
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String[] args) throws Exception
{
java.awt.EventQueue.invokeLater( () -> createAndShowGUI() );
/*
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
*/
}
}
我正在尝试将 JLabel
用作 JTabbedPane
的选项卡,但我不知道该怎么做。可能吗?
如果没有,我正在尝试制作如下所示的标签:
Win10 选项卡:
how to align the text(not the tabs) to the left?
icon/text 的中心对齐由 LAF 控制。我从来没有发现覆盖默认 LAF 行为是一件容易的事,因为您需要为所有平台创建自定义 LAF。
另一种选择是确定用作选项卡组件的每个标签的首选大小,然后将所有这些标签的首选大小设置为相同的宽度。这将强制左对齐。
import javax.swing.*;
import java.awt.*;
public class TabbedPaneLeft extends JPanel
{
private JTabbedPane tabbedPane;
public TabbedPaneLeft()
{
ImageIcon icon = new ImageIcon( "copy16.gif" );
tabbedPane = new JTabbedPane();
tabbedPane.setTabPlacement(JTabbedPane.LEFT);
add( tabbedPane );
initTabComponent(icon, "Tab 1");
initTabComponent(icon, "Tabbed Pane 2");
initTabComponent(icon, "Tab 3");
adjustTabComponentSize();
}
private void initTabComponent(Icon icon, String text)
{
JLabel label = new JLabel( text );
label.setPreferredSize( new Dimension(300, 300) );
tabbedPane.addTab(null, null, label);
JLabel tabLabel = new JLabel( text );
tabLabel.setIcon( icon );
//tabLabel.setHorizontalAlignment(JLabel.LEFT); // doesn't work
//tabLabel.setAlignmentX(0.0f); // doesn't work
tabbedPane.setTabComponentAt(tabbedPane.getTabCount() - 1, tabLabel);
}
private void adjustTabComponentSize()
{
int width = 0;
// Find the width of the larget tab
for (int i = 0; i < tabbedPane.getTabCount(); i++)
{
Dimension d = tabbedPane.getTabComponentAt(i).getPreferredSize();
width = Math.max(width, d.width);
}
// Set the width of all tabs to match the largest
for (int i = 0; i < tabbedPane.getTabCount(); i++)
{
Component tabComponent = tabbedPane.getTabComponentAt(i);
Dimension d = tabComponent.getPreferredSize();
d.width = width;
tabComponent.setPreferredSize( d );
}
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TabbedPaneLeft());
frame.pack();
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String[] args) throws Exception
{
java.awt.EventQueue.invokeLater( () -> createAndShowGUI() );
/*
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
*/
}
}