FlowLayoutについて

このQ&Aのポイント
  • FlowLayoutはJavaのクラスで、JavaのGUIプログラミングにおいてコンポーネント(ボタンやラベルなど)を自動的に配置するためのレイアウトマネージャーです。
  • FlowLayoutクラスは、レイアウトの横方向の配置(左から右へ)や改行の制御が特徴です。
  • FlowLayoutを使用する際には、必要なコンポーネントを順番に追加していくだけで、自動的にレイアウトが調整されます。
回答を見る
  • ベストアンサー

FlowLayoutについて

こんにちは。 FlowLayoutをについて、どうしても理解できないので教えて頂けないでしょうか。 import java.awt.*; import javax.swing.*; public class FlowLayoutEn2 extends JFrame{   public FlowLayoutEn2(){   super("Flow_Layout");   setDefaultCloseOperation(EXIT_ON_CLOSE);   getContentPane().setLayout(new FlowLayout());      JButton first = new JButton("first");   JButton second= new JButton("second");   JButton third = new JButton("third");   getContentPane().add(first);   getContentPane().add(second);   getContentPane().add(third);   pack();   }   public static void main(String args[]){   new FlowLayoutEn2().setVisible(true);   } }  上記でコンパイルすると、「FlowLayoutEn2はpublicであり、ファイルFlowLayoutEn2.javaで宣言しなければなりません。・・・FlowLayoutにアクセスできません・・」と表示されます。  しかし、「import java.awt.*」の部分を、「import java.awt.FlowLayout」に変更すると正常にコンパイルが終了し求めていた結果が表示されます。同じパッケージ内であるの事と、参考書類を見てもFlowLayoutを「import java.awt.*」のみで使用している本もありましたので使用可との認識だったのですが、どうしても理解できません。 基本的な事かもしれませんが、どうぞご教授下さい。

  • Java
  • 回答数2
  • ありがとう数2

質問者が選んだベストアンサー

  • ベストアンサー
  • liar_adan
  • ベストアンサー率48% (730/1515)
回答No.2

>liar_adanさんの環境で、同じ状況になるかを、確認頂けないでしょうか。 やってみたところ、 エラーは出ずにちゃんとコンパイルできました。 不思議ですね。 ためしに、再度「import java.awt.*」に戻してコンパイルしてみてください。 それでエラーが出るかどうか。 ツールの関係で変になっていただけの可能性もあるかもしれません。 エラーが出たら、…どういうことなんでしょうね。 考えにくいけれど、変なディレクトリに同名の "FlowLayout"というクラスがあるのかな…。 たぶん、これを聞いても原因はわからないと思いますが、 念のためにツール名を教えてください。

naka5322
質問者

お礼

liar_adanさん。テストありがとうございました。 私が使用しているツールは、ベクターでダウンロードした 「C Pad for Java2 SDK」というツールです。 ただツールを使用せず、ノートパッドで記述しコンパイルをしても同エラーが発生してしまいます。 でもliar_adanさんの環境でできたという事は、 私のPCだけかもしれないですね。 私の認識が間違いないという事だけわかればOKです。 どうもありがとうございました。

その他の回答 (1)

  • liar_adan
  • ベストアンサー率48% (730/1515)
回答No.1

んー、おかしいですね。 クラス名が重複していない限り、 変わりないはずなんですが。 考えられるのは、変更の際にファイル名を変えていないかどうかです。 Javaでは、(publicなクラスは)そのクラスと同名の .javaという拡張子のソースファイルにして、 コンパイルしなければなりません。 クラス名とファイル名がちがっているとエラーが出ます。 確認してください。

naka5322
質問者

補足

liar_adanさん。早速の返答ありがとうございます。 一応、確認してみたんですけど、クラス名とファイル名は同じです。 (ソース入力/コンパイルする際もツールで行なっていますので、間違いは無いと思います・・) 大変お手数なのですがliar_adanさんの環境で、同じ状況になるかを、確認頂けないでしょうか。 どうぞ、宜しくお願いします。

関連するQ&A

  • finalの意味

    import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ExerciseD8L1{ public static void main(String args[]){ final JFrame myFrame = new JFrame(); myFrame.getContentPane().setLayout(new BorderLayout()); final JLabel myLabel = new JLabel(); JPanel myPanel = new JPanel(); myFrame.getContentPane().add(myLabel,BorderLayout.CENTER); myFrame.getContentPane().add(myPanel,BorderLayout.NORTH); JButton btn1 = new JButton("btn1"); btn1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ myLabel.setText("btn1がクリックされました"); } }); }); myPanel.add(btn1); myFrame.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); myFrame.setSize(400,100); myFrame.setVisible(true); } } これで、finalをつけないとコンパイルできません。 なぜ、fianlが必要なんでしょう。 どのようなときにfinalを用いるのでしょう。 お願いします。

    • ベストアンサー
    • Java
  • JButtonの配置

    FlowLayoutによって、JFrameにボタン6個を2×3に配列するプログラムを考えています。ボタンを配置することはできたのですが、ウインドウの大きさを変えるとボタンが3×2になったりと変更してしまいます。 ウインドウの大きさを変えても、ボタンの大きさは変えず、ボタン6個が2×3配列になるプログラムはどうしたらよいのでしょうか?以下は、途中のプログラムです。 import java.awt.*; import javax.swing.*; public class sample { public static void main(String[] args){ MyFrame frame = new MyFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } public class MyFrame extends JFrame { public static final int WIDTH = 200; public static final int HEIGHT = 300; public MyFrame(){ setSize(WIDTH,HEIGHT); Container contentPane = getContentPane(); MyPanel panel = new MyPanel(); contentPane.add(panel); } } public class MyPanel extends JPanel{ public static final int MESSAGE_X = 200; public static final int MESSAGE_Y = 200; setLayout(new FlowLayout()); JButton button1 = new JButton("Button1"); contentPane.add(button1); JButton button2 = new JButton("Button2"); contentPane.add(button2); JButton button3 = new JButton("Button3"); contentPane.add(button3); JButton button4 = new JButton("Button4"); contentPane.add(button4); JButton button5 = new JButton("Button5"); contentPane.add(button5); JButton button6 = new JButton("Button6"); contentPane.add(button6); }

    • ベストアンサー
    • Java
  • actionListenerについて

    ど素人です 改行の関係でimportは省略してます labelにActionListenerが登録できないのですが、なぜでしょうか class test extends JFrame{ test() { getContentPane().setLayout(new FlowLayout()); JLabel label = new JLabel("てすと"); JButton b1 = new JButton("OK"); b1.addActionListener(label); getContentPane().add(b1); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setTitle("JButtonTest"); setSize(200, 100); setVisible(true); } public static void main(String[] args) { new test(); } } class LetLabel extends JLabel implements ActionListener{ public void LetLabe(){ } public void actionPerformed(ActionEvent ae){ } }

    • ベストアンサー
    • Java
  • javaについて質問

    import javax.swing.*; import java.awt.Color; import java.awt.Graphics; import java.awt.event.*; public class tt extends JFrame { public tt(){ setTitle("hogehoge"); setBounds( 10, 10, 300, 200); JButton btn1 = new JButton("続ける"); final JPanel p = new JPanel(); p.setBackground(Color.white); btn1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event){ } } ); p.add(btn1); getContentPane().add(p); } public static void main(String[] args){ tt test = new tt(); test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); test.setVisible(true); /* 実際に表示する */ } } Graphics g; g.drawString("ほげさんじょう",130,120);を追加しても コンパイルが通りません。 どうすれば画面に文字列を表示するんですか? あとテキストフィールドを追加するさいに、座標していとかして追加できないんですか?

  • ボタンが表示されません

    //テキストパッドをつくっています。 //このプログラムはボタンが表示されませんなぜ? import java.awt.*; import javax.swing.*; class Test_Text extends JFrame{ public static void main(String args[]){ Sample_text st = new Sample_text(); JFrame jf = new JFrame("Test_Text"); JTextArea jt = new JTextArea(50,30); Container c = jf.getContentPane(); c.add(jt,BorderLayout.PAGE_START); jf.pack(); jf.setVisible(true); jf.setDefaultCloseOperation(EXIT_ON_CLOSE); } public void Sample_text(){ JButton j1,j2; j1 = new JButton("編集"); j2 = new JButton("保存"); setLayout(new FlowLayout()); this.add(j1); this.add(j2); } }

  • 参照されない

    クラスSentenceActionperformedのtext1,textの部分がエラーとなります。クラスは呼ばれています。二つとも参照?されていないのです。 参考になるページをみると同じような書き方しているのに問題ないのでどこが違うのかわかりません。この書き方では参照でなきないのだと思いますが正しい書き方がわかりません。別のクラスにしていく方法でいきたいです。 うまく行けばtextの文字がtext1に表示されます。 よろしくお願いします。 import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JTextArea; import javax.swing.JTextField; public class sample extends javax.swing.JFrame{ public sample() { init(); } private void init() { JTextField text = new JTextField(25); JTextArea text1 = new JTextArea(10,25); Container mainFrame = getContentPane(); mainFrame.setLayout(new FlowLayout()); mainFrame.add(text); mainFrame.add(text1); mainFrame.setBounds(0, 0, 500, 500); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); SentenceActionPerformed aa = new SentenceActionPerformed(); text.addActionListener(aa); } class SentenceActionPerformed implements ActionListener{ public void actionPerformed(ActionEvent evt) { text1.setText(text.getText()); } } public static void main(String args[]) { new sample().setVisible(true); } }

    • ベストアンサー
    • Java
  • JAVAでの背景画像表示

    現在javaでゲームのメニュー画面を作っているのですが、ボタンを配置する所まではできたんですが、背景に画像を表示することができなくて困っています! import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JLabel; import javax.swing.ImageIcon; import java.awt.Container; import java.awt.BorderLayout; import javax.swing.border.LineBorder; import javax.swing.border.EtchedBorder; import java.awt.Color; import java.awt.Container; public class Mati extends JFrame{ public static void main(String[] args){ Mati frame = new Mati(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBounds(10, 10,650,650); frame.setTitle("街"); frame.setVisible(true); JPanel h = new JPanel(); h.setOpaque(false); ImageIcon icon1 = new ImageIcon("Mati.jpg"); JLabel label1 = new JLabel(icon1); JLabel label2 = new JLabel(); h.add(label1); } Mati(){ JButton button1 = new JButton("宿屋"); button1.setFont(new Font("Mairyo", Font.PLAIN, 30)); JButton button2 = new JButton("道具屋"); button2.setFont(new Font("Mairyo", Font.PLAIN, 30)); JButton button3 = new JButton("武器屋"); button3.setFont(new Font("Mairyo", Font.PLAIN, 30)); JButton button4 = new JButton("街を出る"); button4.setFont(new Font("Mairyo", Font.PLAIN, 30)); JPanel p = new JPanel(); p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); p.add(Box.createRigidArea(new Dimension(10,30))); p.add(button1); p.add(Box.createRigidArea(new Dimension(5,8))); p.add(button2); p.add(Box.createRigidArea(new Dimension(5,8))); p.add(button3); p.add(Box.createRigidArea(new Dimension(5,8))); p.add(button4); getContentPane().add(p, BorderLayout.CENTER); } } ソースはこのようになっています。これからどうすれば背景画像が表示されるか教えていただきたいです。 よろしくお願いします。

  • 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
  • javaでスタート画面を作っていて困っています。

    現在javaでゲームのスタート画面を作っているのですが、パネルが透過されずに困っています。 プログラムソースは import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JLabel; import javax.swing.ImageIcon; import java.awt.Container; import java.awt.BorderLayout; import javax.swing.*; import java.awt.Font; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.*; import java.awt.event.*; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JLabel; import javax.swing.ImageIcon; import java.awt.Container; import java.awt.BorderLayout; import javax.swing.border.LineBorder; import javax.swing.border.EtchedBorder; import java.awt.Color; import java.awt.Container; class Start最新版 extends JFrame{ public static void main(String args[]){ Start最新版 frame = new Start最新版("タイトル"); frame.setVisible(true); } Start最新版(String title){ setTitle(title); setBounds(10, 10, 1024, 768); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel p = new JPanel(); p.setOpaque( false ); ImageIcon icon1 = new ImageIcon("Start.jpg"); JLabel label1 = new JLabel(icon1); JLabel label2 = new JLabel(); p.add(label1); Container contentPane = getContentPane(); contentPane.add(p, BorderLayout.CENTER); JButton button1 = new JButton("ゲームを始める"); button1.setFont(new Font("Mairyo", Font.PLAIN, 30)); JPanel n = new JPanel(); n.setOpaque(false); n.setLayout(new BoxLayout(n, BoxLayout.Y_AXIS)); n.add(Box.createRigidArea(new Dimension(290,30))); n.add(button1); となっています。「ゲームを始める」ボタンだけを残して、周りのパネルを透過して画像を表示させたいのですが、うまくいきません!解決方法をご存知の方どうか教えていただきたいです!よろしくお願いします。

  • アプレット初心者です。

    アプレット初心者です。 電卓版もどきを作りたいんですが、お手本を見ながらやったはずなのに、コンパイル時に「main」がないという類のエラーが出ます。 どうすれば正常に動くようになるのか、どなたかご指南いただけませんか? よろしくお願いします。 ↓↓以下がソースです。 import java.awt.*; import javax.swing.*; public class D1 extends JApplet{ private JPanel pn; public init(){ JPanel keyPanel=new JPanel(); keyPanel.setLayout(new GridLayout(4,4)); pn.add(keyPanel,BorderLayout.CENTER); keyPanel.add(new JButton("7"),0); keyPanel.add(new JButton("8"),1); keyPanel.add(new JButton("9"),2); keyPanel.add(new JButton("÷"),3); keyPanel.add(new JButton("4"),4); keyPanel.add(new JButton("5"),5); keyPanel.add(new JButton("6"),6); keyPanel.add(new JButton("×"),7); keyPanel.add(new JButton("1"),8); keyPanel.add(new JButton("2"),9); keyPanel.add(new JButton("3"),10); keyPanel.add(new JButton("-"),11); keyPanel.add(new JButton("AC"),12); keyPanel.add(new JButton("0"),13); keyPanel.add(new JButton("="),14); keyPanel.add(new JButton("+"),15); } }