• 締切済み

J#でjava.awt.frameからSystem.Windows.Forms.Formを呼び出す

Windows XP SP2にVisual J# 2005 Express Editionをインストールして使用しています。 今まで、J2SE 5.0を使用していて、GUIの部分はSwingで書いていたのですが、J#に移行したいと思っています。 J#はSwingを一部サポートしているのですが、JTextPaneが使えないので、その部分だけ、J#のSystem.Windows.Forms.FormとSystem.Windows.Forms.RichTextBoxを使って作り替えようと思っています。そこで、java.awt.Frameにjava.awt.Buttonを配置してそのボタンが押されたときにFormを表示させたいのですが、Form.Show()すると表示されたFormがきちんと表示されず、フリーズしてしまいます。 以下に、ソースコードを載せますので、問題点を指摘していただけたら幸いです。 import System.Windows.Forms.Form; import System.Windows.Forms.RichTextBox; import System.Drawing.*; import System.ComponentModel.*; import java.awt.*; import java.awt.event.*; class test implements ActionListener{  Frame frame;  Form f;  RichTextBox tb;  Button b;  IContainer components;  void testGUI(){   this.b=new Button("test");   this.b.addActionListener(this);   this.tb=new RichTextBox();   this.f=new Form();   this.f.get_Controls().Add(this.tb);   this.f.set_Name("Form1");   this.f.set_Text("Form1");   this.frame=new Frame();   this.frame.setSize(100,100);   this.frame.add(this.b);   this.frame.show();  }  public static void main(String[] args){   test2 t2=new test2();   t2.testGUI();  }  private void f_Load(Object sender, System.EventArgs e){}  public void actionPerformed(ActionEvent ae){   if(ae.getSource()==this.b){    this.f.Show();   }  } }

みんなの回答

noname#49664
noname#49664
回答No.1

コードを見ましたが、これ、.netのFormにAWTのButtonを組み込もうとしてませんか? .netのコンポーネントとAWTなどpure Javaコンポーネントを混ぜて使うことはできないと思いますよ。また.netのコンポーネントにjava.awt.eventのイベントを組み込もうとしたりしていませんか? これも無理でしょう。  .netとpure JavaのGUIライブラリは全く別物ですから両者を混ぜて使うことはできません。まずは.netかAWTかどちらか一方だけを使うように書き直してみてください。

yamamo2
質問者

お礼

今回の質問をMSDN Forum Visual J# GeneralにポストしたところVisual Studioの開発チームの方から返事があり、J#のバグであることが判明しました。

yamamo2
質問者

補足

> コードを見ましたが、これ、.netのFormにAWTのButtonを組み込も > うとしてませんか? > .netのコンポーネントにjava.awt.eventのイベントを組み込もう > としたりしていませんか? いいえ、コードの通りAWTと.NETはそれぞれ分離させてあり、混ぜてはいません。さすがに混ぜてしまうとコンパイル自体が通らないので・・・。 また、その混同をさけるためにimportで.netのFormとRichTextBoxを明示させています。

全文を見る
すると、全ての回答が全文表示されます。

関連するQ&A

  • カレンダーを作っているのですが

    import java.awt.*; import java.awt.event.*; public class GraphicsC4 extends Frame { public static void main(String ar[]){ Frame f=new GraphicsC4(); f.setTitle ("平成19年6月 (GridLayout)"); f.setSize(640,400); f.setVisible(true); } GraphicsC4(){ setFont(new Font("SansSerif",Font.BOLD+Font.ITALIC,30)); GridLayout gl=new GridLayout(6,7); setLayout(gl); String day[]={"SUN","MON","TUE","WED","THU","FRI","SAT"}; for(int j=0;j<=6;j++){ Button b1=new Button(day[j]); add(b1); } for(int e=1;e<=5;e++){ Button b2=new Button(""); add(b2); } for(int i=1;i<=30;i++){ Button b3=new Button(""+i+""); add(b3); } addWindowListener(new WinAdapter()); } class WinAdapter extends WindowAdapter{ public void windowClosing(WindowEvent we){System.exit(0);} } } ここまで書いたのですが、日曜日を赤く表示することが出来ません。どなたか教えてください。

  • AWTでウィンドウを閉じる仕組み

    import java.awt.*; import java.awt.event.*; class WindowClose extends WindowAdapter{ public void windowClosing(WindowEvent we){ System.exit(0); } } class Figure extends Frame{ public Figure(String name){ setTitle(name); setLocation(300,200); setSize(300,200); setBackground(Color.ORANGE); WindowClose wc = new WindowClose(); addWindowListener(wc); } } public class Test{ public static void main(String [] args){ Figure figure = new Figure("Frame1"); figure.show(); } } 初歩的なことな質問で申し訳ないです。 このような処理でウィンドウを閉じているのですが、具体的に何を取得してどんな動作をしているのかがよくわかりません。 wcというインスタンスにウィンドウリスナーのオブジェクトが追加登録されるという感じだと思いますが、extends されているWindowAdapterクラスにはすでにウィンドウリスナーのインスタンスが実装されていますよね。一体何の意味があるのでしょうか。

    • ベストアンサー
    • Java
  • return;

    retrun;を取ると動作がおかしくなるんですが return;にはどういう働きがあるんでしょうか? お願いします。 import java.applet.Applet; import java.awt.*; import java.awt.event.*; //<applet code = "a.class" width = "300" height = "300"></applet> public class a extends Applet implements ActionListener{ Dialog d; Frame f; public void init() { setLayout( new GridLayout( 1, 1 ) ); Button b = ( Button )add( new Button( "frame" ) ); b.addActionListener( this ); } public void actionPerformed( ActionEvent e ){ if( e.getActionCommand() == "frame" ){ if ( f == null ){ f = new Frame( "Kitty on your lap" ); Button fb = (Button)f.add( new Button( "Kitty " ) ); fb.addActionListener( this ); f.setSize( 200 , 200 ); f.setVisible( true ); }else if( d == null ){ f.dispose(); f = null; } return; } if( e.getActionCommand() == "OK" ) { d.dispose(); d = null; return; } d = new Dialog( f, "Kitty", true ); d.setLayout( new GridLayout( 2, 1 ) ); d.setResizable( false ); d.add( new Label( "Kitty on your lap" ) ); Button b = (Button)d.add( new Button( "OK" ) ); b.addActionListener( this ); d.setSize( 400 , 200 ); d.setVisible( true ); } }

    • ベストアンサー
    • Java
  • java スロット

    ボタンj1を押すと、ラベルtlの数字だけを停止させるようにし、tl2,tl3は数字を表示したい のですが、 現在ボタンj1を押すと、Exception in thread "Thread-2" Exception in thread "Thread-4" java.lang.IllegalMonitorStateException・・・と例外が発生し、プログラムが停止します。 以下がプログラムです。 import java.awt.*; import java.awt.event.*; import java.util.*; public class AWT extends Frame{ int i=0; String str; Button j1; int i2=0; TL tl; boolean pending=false; public AWT(){ super(); this.setSize(800,700); j1=new Button("AWT"); j1.addActionListener(new MyButtonActionAdapter()); Button j2=new Button("AWT2"); Button j3=new Button("AWT3"); this.addWindowListener(new WA()); this.setVisible(true); this.setLayout(new GridLayout(2,3)); tl =new TL(1); this.add(tl); TL tl2=new TL(10); this.add(tl2); TL tl3=new TL(30); this.add(tl3); this.add(j1); this.add(j2); this.add(j3); } public static void main(String[] args){ AWT test=new AWT(); test.validate(); } class WA extends WindowAdapter{ public void windowClosing(WindowEvent ev){ System.exit(0); } }/*class WAの終わり*/ class TL extends Label implements Runnable{ Random rnd = new Random(); TL(long seed){ this.setText("count" ); this.setFont(new Font("SansSerif",Font.BOLD,18)); this.setForeground(Color.blue); rnd.setSeed(seed); new Thread(this).start(); } public void run(){ while(true){ i2=rnd.nextInt(3)+1; this.setText(String.valueOf(i2)); try{ Thread.sleep(1000); if(pending){ wait(); } }catch(InterruptedException e){ e.printStackTrace(); }finally{ } } }/*runの終わり*/ public synchronized void setpending(boolean f){ pending=f; } }/*class TLの終わり*/ class MyButtonActionAdapter implements ActionListener{ public void actionPerformed(ActionEvent ev){ j1.setLabel("CLICKED"); tl.setpending(!pending); } }/*class MY・・・の終わり*/ }/* class AWTの終わり*/ ご指導お願いします。

    • ベストアンサー
    • Java
  • 下のソースを参考書をそのまま書いたのですが

    下のソースを参考書をそのまま書いたのですが eclipse環境だと f.show(); のところが推薦されない使われ方 として実行できません 無理やり動作させたところ、エラーが返ってきました どうすれば、f.show(); が有効にできるでしょうか? import java.awt.*; import java.awt.event.*; import java.applet.Applet; public class window { public static void main( String[] args ) { AppFrame f = new AppFrame(); f.setSize( 200, 200 ); f.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e) { System.exit(0); } } ); f.show(); } } class AppFrame extends Frame implements ActionListener { Button b1, b2; labelWindow window; AppFrame() { setLayout( new FlowLayout() ); b1 = new Button( "Display the window" ); add(b1); b1.addActionListener( this ); b2 = new Button( "Hide the window" ); add(b2); b2.addActionListener( this ); window = new labelWindow(this); window.setSize( 300,200 ); window.setLocation( 300, 300 ); } public void actionPerformed( ActionEvent event ) { if( event.getSource() == b1 ) { window.setVisible(true); } if( event.getSource() == b2 ) { window.setVisible(false); } } } class labelWindow extends Window { Label label; labelWindow( AppFrame af ) { super( af ); setLayout( new FlowLayout() ); label = new Label( "Hello from Java!" ); add( label ); } public void paint( Graphics g ) { int width = getSize().width; int heigth = getSize().heigth; g.drawRect( 0, 0, --width, --heigth ); } }

    • ベストアンサー
    • Java
  • GridLayoutのPanel上のButton

    GridLayoutのPanel上のButton横サイズを取得したいのですが、 button1.getWidth()==0 と出てしまいます。 フレームやパネルのサイズをボタン数で割れば出るのですが、 そうではなく、ボタンのサイズを取得したいです。 ご存知の方がおられましたら教えて下さい。 //----以下ソースです---- import java.awt.Dimension; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class ButtonSizeOnGridLayout extends JFrame { public ButtonSizeOnGridLayout() { JPanel panel1 = new JPanel(new GridLayout(0, 5)); JButton button1 = new JButton(); button1.setPreferredSize(new Dimension(150, 150)); System.out.println("button1.getWidth()==" + button1.getWidth());//出力:0 panel1.add(button1); this.getContentPane().add(panel1); this.pack(); this.setVisible(true); } public static void main(String a[]) { new ButtonSizeOnGridLayout(); } }

    • ベストアンサー
    • Java
  • javaのmainの中のループに割込を掛ける

    毎度、お世話になります。 javaのmainの中のループに割込を掛ける方法をお教えください。 添付コードの『Thread.currentThread().interrup()』は、旨く機能しません。 以上、宜しくお願いします。 =========== import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class T_try_loop extends JFrame{ JFrame frame=new JFrame(); T_try_loop(){ System.out.println("aaaa"); JPanel p1=new JPanel(); JButton button1=new JButton("button1"); button1.addActionListener(new TimButton1()); p1.add(button1); getContentPane().add(p1, BorderLayout.CENTER); } public static void main(String args[]){ T_try_loop frame=new T_try_loop(); frame.setTitle("TTTT"); frame.setBounds(10,10,400,300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); for(int j = 0;j < 80; j++){ try{ Thread.sleep(80); System.out.print("*"); } catch(InterruptedException e){ System.out.println(": main_loopに、今割り込まれました"); } } }//public static void main public class TimButton1 implements ActionListener{ @Override public void actionPerformed(ActionEvent ae){ String cmd =ae.getActionCommand(); if(cmd.equals("button1")){ Thread.currentThread().interrupt(); System.out.println("Button clicked"); } } }//public class TimButton1 }

    • ベストアンサー
    • Java
  • javaのBoxlayoutについて

    下記はBoxLayout用ののコードです。 Q1)Buttonのサイズを設定する方法がありますか? Q2)Buttonの配置を設定する方法がありますか? //===================================== import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.LayoutManager; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class AlignmentX extends JFrame{ JButton button1; JButton button2; JButton button3; public static void main(String[] args){ AlignmentX frame = new AlignmentX(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("タイトル"); frame.setVisible(true); } AlignmentX(){ button1 = new JButton("AAAAA"); button1.setAlignmentY(0.5f); button1.setPreferredSize(new Dimension(80,20)); button2 = new JButton("BBB"); button2.setAlignmentY(0.5f); button2.setPreferredSize(new Dimension(80,20)); button3 = new JButton("CC"); button3.setAlignmentY(0.5f); button3.setPreferredSize(new Dimension(80,20)); JPanel p = new JPanel(); p.setLayout((LayoutManager) new BoxLayout(p, BoxLayout.Y_AXIS)); p.add(button1); p.add(button2); p.add(button3); getContentPane().add(p, BorderLayout.CENTER); setBounds(10, 10, 300, 200); } } //以上,宜しくお願いします。

    • ベストアンサー
    • Java
  • ボタンとダイアログの関連性

    import java.awt.*; import java.awt.event.*; public class ActionListenerTest extends Frame implements ActionListener { ActionListenerTest() { super("ActionListenerTest"); Button b1 = new Button("BUTTON"); b1.addActionListener(this); add(b1); setSize(200, 100); setVisible(true); } public void actionPerformed(ActionEvent evt) { String ac = evt.getActionCommand(); if( ac == "BUTTON") { /*ここでの操作でダイアログを表示させたい*/ } } public static void main(String [] args) { new ActionListenerTest(); } } 表示されたフレーム内のボタンを押したときに、ダイアログを表示させたいです。コメントアウトした部分でダイアログを表示させたいのですが、ダイアログの定義はActionListenerTest() 内にて行うのでしょうか? またダイアログ内でボタンを作り、そのボタンの操作を行うときは、フレームでのボタンと同様にString ac = evt.getActionCommand(); ---でいいのでしょうか? どなたかご教授願います。 汚いプログラムすいません。

    • ベストアンサー
    • Java
  • HTMLファイルを開きたい

    こんにちは、いつもお世話になっています。 質問があります。 アプレットでボタンを押すと、 HTMLファイル「c:/test_folder/test.html」 を開きたいのですが、 下記コードの(☆) のところに何を書いていいかわかりません。 どなたかご存知でしたら教えていただけないでしょうか? よろしくお願いします。 ============================================================= import javax.swing.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import java.net.*; public class situmon extends Applet implements ActionListener { JPanel panel; JButton button; public void init() { panel=new JPanel(); button=new JButton("次へ"); button.addActionListener(this); panel.add(button); this.add(panel); } public void actionPerformed(ActionEvent e) { if(e.getSource()==button) { (☆); } } }

    • ベストアンサー
    • Java