SwingがいちおうAWTと共存して利用できるようになっているが、だめなパターンがある。どうやら、SwingのJMenuの類(JManuBarとか)とAWTのPanelをいっしょに使うととPanelがJMenuを隠してしまう。
とりあえず、EclipseのVisual Editorを使って、やっつけでサンプルを作ってみた。JFrameにJMenuとJTabbedPaneを置いて、そのうえにJPanelを置いたものである。 上の画像のウィンドウは、メニューはちゃんと表示されていて、これは特に問題ない。
つぎの画像は、JPanelの上にPanelを置いたものである。見た目あまりわかりづらいかもしれないが、メニューのセパレーターの下からが隠れてしまっている。AWTのPanelがメニューの上にかぶさってしまって、隠れて見えなくなっている。JTabbedPaneの青いところの上にメニューがあるようなので、Panelだけがメニューを隠しているようだ。
参考になるコードではないが、折角作ったので載せておくw。
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;
import javax.swing.JTabbedPane;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.Label;
import java.awt.Panel;
/**
* Main.java
* Swing and AWT Component test
*
* @author finky
*
*/
public class Main extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JMenuBar frameMenuBar = null;
private JMenu fileMenu = null;
private JMenuItem openMenuItem = null;
private JTabbedPane jTabbedPane = null;
private JPanel jPanel = null;
private JLabel jLabel = null;
private JMenuItem exitMenuItem = null;
private JPanel jPanel1 = null;
private Panel panel = null;
private Label label = null;
/**
* This method initializes frameMenuBar
*
* @return javax.swing.JMenuBar
*/
private JMenuBar getFrameMenuBar() {
if (frameMenuBar == null) {
frameMenuBar = new JMenuBar();
frameMenuBar.add(getFileMenu());
}
return frameMenuBar;
}
/**
* This method initializes fileMenu
*
* @return javax.swing.JMenu
*/
private JMenu getFileMenu() {
if (fileMenu == null) {
fileMenu = new JMenu();
fileMenu.setText("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
fileMenu.add(getOpenMenuItem());
fileMenu.add(new JSeparator());
fileMenu.add(getExitMenuItem());
}
return fileMenu;
}
/**
* This method initializes openMenuItem
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getOpenMenuItem() {
if (openMenuItem == null) {
openMenuItem = new JMenuItem();
openMenuItem.setText("Open");
openMenuItem.setMnemonic(KeyEvent.VK_O);
}
return openMenuItem;
}
/**
* This method initializes jTabbedPane
*
* @return javax.swing.JTabbedPane
*/
private JTabbedPane getJTabbedPane() {
if (jTabbedPane == null) {
jTabbedPane = new JTabbedPane();
jTabbedPane.addTab("Panel1", null, getJPanel(), null);
jTabbedPane.addTab("Panel2", null, getJPanel1(), null);
}
return jTabbedPane;
}
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
gridBagConstraints.weighty = 1.0;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.gridy = 0;
jLabel = new JLabel();
jLabel.setText("This is JPanel");
jPanel = new JPanel();
jPanel.setLayout(new GridBagLayout());
jPanel.add(jLabel, gridBagConstraints);
}
return jPanel;
}
/**
* This method initializes exitMenuItem
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getExitMenuItem() {
if (exitMenuItem == null) {
exitMenuItem = new JMenuItem();
exitMenuItem.setText("Exit");
exitMenuItem.setMnemonic(KeyEvent.VK_X);
exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
dispose();
}
});
}
return exitMenuItem;
}
/**
* This method initializes jPanel1
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel1() {
if (jPanel1 == null) {
GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
gridBagConstraints1.gridx = 0;
gridBagConstraints1.fill = GridBagConstraints.BOTH;
gridBagConstraints1.weightx = 1.0;
gridBagConstraints1.weighty = 1.0;
gridBagConstraints1.gridy = 0;
jPanel1 = new JPanel();
jPanel1.setLayout(new GridBagLayout());
jPanel1.add(getPanel(), gridBagConstraints1);
}
return jPanel1;
}
/**
* This method initializes panel
* This panel hides Menus.
* @return java.awt.Panel
*/
private Panel getPanel() {
if (panel == null) {
GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
gridBagConstraints2.weightx = 1.0;
gridBagConstraints2.anchor = GridBagConstraints.NORTHWEST;
gridBagConstraints2.weighty = 1.0;
label = new Label();
label.setText("This is Panel on JPanel");
panel = new Panel();
panel.setLayout(new GridBagLayout());
panel.add(label, gridBagConstraints2);
}
return panel;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Main thisClass = new Main();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
/**
* This is the default constructor
*/
public Main() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(300, 200);
this.setJMenuBar(getFrameMenuBar());
this.setContentPane(getJContentPane());
this.setTitle("Swing Test");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getJTabbedPane(), BorderLayout.CENTER);
}
return jContentPane;
}
}
0 件のコメント:
コメントを投稿