次のコードに示すように、プログラムの開始時にアプリケーション ウィンドウをロードするメイン クラスがあります。
public class SwendaEye{
public static void main(String[]args){
FrameandComp frame = new FrameandComp();
JFrame win;
win = frame.mainFrame();
/*========================================================================*/
JMenuBar bar;
bar = new JMenuBar();
win.setJMenuBar(bar);
JMenu swenda = new JMenu("SWENDAEYE");// adding Swenda menu to the bar.
bar.add(swenda);
JMenuItem open = new JMenuItem("Open");
swenda.add(open);
JMenuItem exit = new JMenuItem("Exit");
swenda.add(exit);
JMenu tools = new JMenu("Tools");// adding Tools menu to the bar.
bar.add(tools);
JMenuItem convertIP = new JMenuItem("Convert IP Address");
tools.add(convertIP);
JMenuItem convertDomain = new JMenuItem("Convert Domain Name");
tools.add(convertDomain);
Domain dom = new Domain();
dom.iniTheEvent(convertDomain);
convertDomain.addActionListener(dom);
JMenu view = new JMenu("View"); // adding Swenda menu to the bar.
bar.add(view);
JMenuItem webDetail = new JMenuItem("Website header");
view.add(webDetail);
JMenuItem report = new JMenuItem("Report");
view.add(report);
win.setVisible(true);
}
}
Domain
たとえば、ドメイン コンバーターをクリックすると、クラスに設定されている新しいウィンドウをトリガーできるはずです。私の場合、ウィンドウは開きますが、「ドメインコンバーター」ウィンドウには 、ボタンとテキストフィールドのみが必要なときにクラスMenu
で定義された が含まれています。Main
ご覧のとおり、クラスのどこにもMenu
またはを実装または開始していません。MenuItem
Domain
public class Domain implements ActionListener{
FrameandComp frame = new FrameandComp();
JFrame domFrame;
String DomainTitle= "Domain Converter";
TextField text = new TextField(20);
JButton bot = new JButton("Convert");
JMenuItem men;
public Domain(){
}
public void createFrame(){
FlowLayout flow = new FlowLayout(); // Create a layout manager
domFrame =frame.setFrame(400,200,DomainTitle,JFrame.DISPOSE_ON_CLOSE);
Container content = domFrame.getContentPane(); // Get the content pane
content.setLayout(flow);
content.add(text);
content.add(bot);
domFrame.add(content);
domFrame.setVisible(true);
}
public JMenuItem iniTheEvent(JMenuItem menuIt){
return men = menuIt;
}
public void actionPerformed(ActionEvent event) {
if(event.getSource() == men ){
this.createFrame();
}
}
}
メイン ウィンドウからメニュー機能を継承せずに、ドメイン コンバータをクリックしてMenuItem
新しいウィンドウを開くことができるようにしたいと考えています。
新しいウィンドウがメイン ウィンドウのメニューを継承する可能性があるアプリケーションで間違ったことを誰か教えてもらえますか?
あなたのサポートは非常に高く評価されます。
このFrameandComp
クラスは、以下のすべてを初期化するために使用されますJFrames
。
public class FrameandComp {
static JFrame awindow = new JFrame();
public FrameandComp(){}
public JFrame setFrame(int width, int height, String title, int exitRule){
Toolkit thekit = awindow.getToolkit();
Dimension wndsize = thekit.getScreenSize();
awindow.setBounds(wndsize.width/4,wndsize.height/4,width, height);
awindow.getContentPane().setBackground(Color.DARK_GRAY);
awindow.setTitle(title);
awindow.setDefaultCloseOperation(exitRule);
return awindow;
}
public JFrame mainFrame(){
Toolkit thekit = awindow.getToolkit();
Dimension wndsize = thekit.getScreenSize();
awindow.setBounds(wndsize.width/4,wndsize.height/4,
((2 *wndsize.width)/2), ((2 *wndsize.height)/2));
awindow.getContentPane().setBackground(Color.DARK_GRAY);
awindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
awindow.setTitle("SWENDAEYE");
return awindow;
}
}