Javaのswingで、ファイルを選択してそのファイルパスを引数として渡すプログラムを作成しています。
ファイルを選択する処理は、ファイルチューザーを使用して実現できましたが、その選択したファイルパスを
テキストフィールドに表示させることができません。
現時点ではそもそもテキストフィールドも表示されていない状況です。。
swing初心者のため、ここからどのように実現すればよいのか困っています。
有識者の方々、教えてください。
以下、現在のソースです。
public class jFileFrame extends JFrame implements ActionListener {
JLabel label ;
public static void main(String[] args){
jFileFrame frame = new jFileFrame();
frame.setBounds(400, 400, 600, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("サンプル");
frame.setVisible(true);
}
@Override public void actionPerformed(ActionEvent e) { File dir = new File("C:\\work\\makeInstall"); JFileChooser filechooser = new JFileChooser(dir); int selected = filechooser.showOpenDialog(this); if(selected == JFileChooser.APPROVE_OPTION){ File file = filechooser.getSelectedFile(); label.setText(filechooser.getName(file)); } } jFileFrame(){ JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(null); JButton button = new JButton("設定ファイル選択"); button.addActionListener(this); button.setBounds(400, 50, 170, 35); JButton button2 = new JButton("情報ファイル選択"); button2.addActionListener(this); button2.setBounds(400, 100, 170, 35); JButton button3 = new JButton("定義ファイル雛形選択"); button3.addActionListener(this); button3.setBounds(400, 150, 170, 35); JButton button4 = new JButton("プログラム雛形選択"); button4.addActionListener(this); button4.setBounds(400, 200, 170, 35); buttonPanel.add(button); buttonPanel.add(button2); buttonPanel.add(button3); buttonPanel.add(button4); label = new JLabel(); JPanel labelPanel = new JPanel(); labelPanel.setLayout(null); JTextField text2 = new JTextField(); text2.setBounds(10, 50, 500, 35); //labelPanel.add(label); labelPanel.add(text2); getContentPane().add(labelPanel, BorderLayout.CENTER); getContentPane().add(buttonPanel, BorderLayout.CENTER); }
}
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。