Hello Friends我正在使用Swing和awt创建桌面应用程序。我正在使用JTabbedPane来创建Tab(活动,公园,过滤轨迹,MyPark)但它在左边对齐我希望它在中心对齐。我使用一些自定义的UL(外观和感觉),但我没有得到任何解决方案和想法这里是我的代码。
private void initTabbedPane() {
UIManager.put("TabbedPane.selected", Color.BLUE);
tabbedPane = new JTabbedPane(){
public Color getForegroundAt(int index){
if(getSelectedIndex() == index) return Color.white;
return Color.BLACK;
}
};
tabbedPane.setUI(new CustomTabUI());
tabbedPane.setBackground(Color.WHITE);
tabbedPane.setTabPlacement(JTabbedPane.TOP);
tabbedPane.addTab(getOutdoors.getActivities().getName(),
new ActivityTab(getOutdoors.getActivities()));
tabbedPane.addTab(getOutdoors.getParks().getName(), new ParkTab(getOutdoors.getParks()));
tabbedPane.addTab("Filter Trails", new FilterTab(getOutdoors.getActivities(),
getOutdoors.getTrailLists(), getOutdoors.getNumTrailLists()));
for (int i = 0; i < getOutdoors.getNumTrailLists(); i++) {
TrailList t = getOutdoors.getTrailList(i);
if(t != null)
tabbedPane.addTab(t.getParkName(), new TrailListTab(t, getOutdoors.getActivities()));
}
tabbedPane.setSelectedIndex(0);
if (openFile) {
getContentPane().removeAll();
openFile = false;
}
tabbedPane.setTabPlacement(JTabbedPane.TOP);
tabbedPane.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);
getContentPane().add(tabbedPane, BorderLayout.CENTER);
getContentPane().validate();
}
自定义UI类: -
public class CustomTabUI extends BasicTabbedPaneUI {
private final Insets borderInsets = new Insets(0, 0, 10, 0);
@Override
protected void paintContentBorder(Graphics g, int tabPlacement, int selectedIndex) {
}
@Override
protected Insets getContentBorderInsets(int tabPlacement) {
return borderInsets;
}
}
答案 0 :(得分:0)
好的我知道我要提出的是某种欺骗行为。不是一个合适的解决方案,但你可以使用它
所以我的代码如下
public class NewJFrame extends javax.swing.JFrame {
/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
jTabbedPane1.addTab(" ", null);
jTabbedPane1.setBackgroundAt(0, new Color(0,102,102));
jTabbedPane1.setEnabledAt(0, false);
JPanel jPanel;
jPanel = new JPanel();
jPanel.setBackground(Color.yellow);
jTabbedPane1.addTab("My tab 1", jPanel);
jTabbedPane1.setSelectedIndex(1);
jPanel = new JPanel();
jPanel.setBackground(Color.PINK);
jTabbedPane1.addTab("My tab 1", jPanel);
jTabbedPane1.addTab(" ", null);
jTabbedPane1.setEnabledAt(3, false);
jTabbedPane1.setBackgroundAt(3, new Color(0,102,102));
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jTabbedPane1 = new javax.swing.JTabbedPane();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(new java.awt.Color(0, 102, 102));
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 920, Short.MAX_VALUE)
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 478, Short.MAX_VALUE)
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
boolean state = false ;
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JPanel jPanel1;
private javax.swing.JTabbedPane jTabbedPane1;
// End of variables declaration
}
你必须删除灵气的外观和感觉,你可以改善这一点,并删除禁用标签的边框为绿色,这样它就不会寻找用户眼睛
这只是一个骗子