JFrameの装飾を解除して、上部に閉じるボタンのみを表示するか、それに似たものを表示できるかどうかを知りたい..どんな助けでも大歓迎です。詳細: 実際、私は GUI ベースのソフトウェアであるプロジェクトを作成しています。その中で JdesktopPane を使用し、JInternalFrames を使用しているため、上部にタイトルバーを表示することは私には適していません。私の問題を解決する方法はありますか。
質問する
4121 次
1 に答える
3
基本的に、コマ枠を削除すると、移動も含めてその機能を担当します。
この例では、基本的に を使用しJPanel
て「タイトル」ペインのプレースホルダーとして機能させ、 を使用BorderLayout
してタイトルとコンテンツをレイアウトします。
実際の閉じるボタンは、JButton
いくつかの画像を使用して閉じるアクションを表す単純なものです。明らかに、これは Windows でのみ正しく表示されます。これらのボタンは通常、OS 自体によって生成されます...そして、そのレイヤーにアクセスすることはできません...
public class TestUndecoratedFrame {
public static void main(String[] args) {
new TestUndecoratedFrame();
}
public TestUndecoratedFrame() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
TitlePane titlePane = new TitlePane();
JFrame frame = new JFrame("Testing");
frame.setUndecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
((JComponent)frame.getContentPane()).setBorder(new LineBorder(Color.BLACK));
frame.add(titlePane, BorderLayout.NORTH);
frame.add(new JLabel("This is your content", JLabel.CENTER));
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TitlePane extends JPanel {
public TitlePane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.anchor = GridBagConstraints.EAST;
add(new CloseControl(), gbc);
}
}
public class CloseControl extends JButton {
public CloseControl() {
setBorderPainted(false);
setContentAreaFilled(false);
setFocusPainted(false);
setOpaque(false);
setBorder(new EmptyBorder(0, 0, 8, 12));
try {
setRolloverIcon(new ImageIcon(ImageIO.read(getClass().getResource("/Highlighted.png"))));
setRolloverEnabled(true);
setIcon(new ImageIcon(ImageIO.read(getClass().getResource("/Normal.png"))));
} catch (IOException ex) {
Logger.getLogger(TestUndecoratedFrame.class.getName()).log(Level.SEVERE, null, ex);
}
addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SwingUtilities.getWindowAncestor(CloseControl.this).dispose();
}
});
}
}
}
于 2013-03-02T04:40:21.597 に答える