Java 使用Nimbus look and feel时,JToolBar中的JButton不可见
我想知道是否有人能想出一个解决以下布局问题的办法,我正在使用Java 使用Nimbus look and feel时,JToolBar中的JButton不可见,java,swing,nimbus,jtoolbar,Java,Swing,Nimbus,Jtoolbar,我想知道是否有人能想出一个解决以下布局问题的办法,我正在使用Nimbuslook-and-feel 问题在于,工具栏中的按钮不可见,因为布局管理器没有正确说明JTextField的宽度。金属外观似乎没有显示此缺陷 import java.awt.*; import javax.swing.*; public class TextFieldTest extends JFrame { public TextFieldTest() { // Create the te
Nimbus
look-and-feel
问题在于,工具栏中的按钮不可见,因为布局管理器没有正确说明JTextField
的宽度。金属
外观似乎没有显示此缺陷
import java.awt.*;
import javax.swing.*;
public class TextFieldTest extends JFrame
{
public TextFieldTest()
{
// Create the text field
JTextField textField = new JTextField( 20 )
{
@Override
public Dimension getMaximumSize()
{
return super.getPreferredSize();
}
};
// Create the tool bar
JToolBar toolBar = new JToolBar();
toolBar.add( textField );
toolBar.add( Box.createHorizontalGlue() );
toolBar.add( new JButton( "Button" ) );
// Layout the frame
getContentPane().setLayout( new BorderLayout() );
getContentPane().add( toolBar, BorderLayout.NORTH );
setPreferredSize( new Dimension( 800, 600 ) );
pack();
}
public static void main( String[] args )
{
SwingUtilities.invokeLater( new Runnable()
{
public void run()
{
TextFieldTest test = new TextFieldTest();
test.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
test.setVisible( true );
}
} );
}
}
非常感谢您的建议。Nimbus中JToolBar的默认布局为:
class javax.swing.plaf.synth.SynthToolBarUI$SynthToolBarLayoutManager
您应该设置它:
toolBar.setLayout(new BoxLayout(toolBar, BoxLayout.X_AXIS));
这种方法的唯一问题是:(1)工具栏控制柄消失(当其可浮动时),以及(2)当工具栏被拖动为垂直时,还必须切换到Y_轴。但由于我的工具栏是不可浮动的,它不会影响我。谢谢你的帮忙。