Java で ボタンを押して、エディットボタンのテキストを取得して
コンソールに出力したいのですが、
MouseEvent から JEditorPane にキャストすると失敗して
テキスト情報を取得できません。
どのような方法で MouseEvent から JEditoPane の情報(getText)を取得できるのでしょうか?
以下がソースコードとエラーメッセージです。
[環境]
OS:Windows 10 Pro
言語:Java + Swing
IDE:Eclipse
Java
1[JAppWindow.java] 2import java.awt.EventQueue; 3import java.awt.event.ActionEvent; 4import java.awt.event.ActionListener; 5import java.awt.event.MouseAdapter; 6import java.awt.event.MouseEvent; 7 8import javax.swing.JButton; 9import javax.swing.JEditorPane; 10import javax.swing.JFrame; 11import javax.swing.JLabel; 12 13public class JAppWindow { 14 15 private JFrame frame; 16 17 /** 18 * Launch the application. 19 */ 20 public static void main(String[] args) { 21 EventQueue.invokeLater(new Runnable() { 22 public void run() { 23 try { 24 JAppWindow window = new JAppWindow(); 25 window.frame.setVisible(true); 26 } catch (Exception e) { 27 e.printStackTrace(); 28 } 29 } 30 }); 31 } 32 33 /** 34 * Create the application. 35 */ 36 public JAppWindow() { 37 initialize(); 38 } 39 40 /** 41 * Initialize the contents of the frame. 42 */ 43 private void initialize() { 44 frame = new JFrame(); 45 frame.setBounds(100, 100, 450, 300); 46 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 47 frame.getContentPane().setLayout(null); 48 49 JEditorPane dtrpnHttpwwwgooglecojp = new JEditorPane(); 50 dtrpnHttpwwwgooglecojp.setText("http://www.google.co.jp"); 51 dtrpnHttpwwwgooglecojp.setBounds(117, 16, 182, 19); 52 frame.getContentPane().add(dtrpnHttpwwwgooglecojp); 53 54 JButton button = new JButton("検索"); 55 button.addMouseListener(new MouseAdapter() { 56 @Override 57 public void mousePressed(MouseEvent arg0) { 58 System.out.println("mousePressed."); 59 String szURL; 60 JEditorPane temp = (JEditorPane)arg0.getSource(); // キャストできない 61 szURL = temp.getText(); 62 System.out.println(szURL); 63 } 64 }); 65 button.addActionListener(new ActionListener() { 66 public void actionPerformed(ActionEvent arg0) { 67 } 68 }); 69 button.setBounds(311, 14, 91, 21); 70 frame.getContentPane().add(button); 71 72 JLabel label = new JLabel("キーワード"); 73 label.setBounds(12, 22, 91, 13); 74 frame.getContentPane().add(label); 75 76 frame.setVisible(true); 77 } 78} 79
Java
1[セイバーたんの画像をダウンロードする.java] 2public class セイバーたんの画像をダウンロードする { 3 4 public static void Step1() 5 { 6 } 7 8 public static void Step2() 9 { 10 } 11 public static void main(String[] args) { 12 // TODO 自動生成されたメソッド・スタブ 13 DownloadAndSave node = new DownloadAndSave(); 14 node.setInfo( "https://tsundora.com/image/2019/09/fate_grand_order_5650.jpg", "fate_5650.jpg" ); 15 node.go(); 16 17 node.setInfo( "https://www.yahoo.co.jp", "yahoo.txt" ); 18// node.go(); 19 20 node.setInfo( "https://tsundora.com/343570", "test.htm" ); 21// node.go(); 22 23 node.setInfo( "https://tsundora.com/image/2019/09/fate_grand_order_5779.jpg", "test.jpg" ); 24// node.go(); 25 26 JAppWindow window = new JAppWindow(); 27 } 28 29} 30
エラーメッセージ(コンソール)
HTTP_OK
DownloadAndSave - END -
mousePressed.
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JButton cannot be cast to javax.swing.JEditorPane
at JAppWindow$2.mousePressed(JAppWindow.java:59)
at java.awt.AWTEventMulticaster.mousePressed(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/11/01 08:25