プロが教えるわが家の防犯対策術!

はじめまして、ただいまjavaを勉強しています。
ラジオボタンを用いて画像を切り替え、かつマウスドラッグによる画像のスクロールを実行するプログラムを作成しました。しかし、コンパイルは成功するのですが実行すると画像が表示されません。以下にソースコードを載せます。

/***********
MainPanel2.java
*************/

import java.net.MalformedURLException;
import java.net.URL;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.File;
import java.applet.Applet;


public class MainPanel2 extends JApplet implements ActionListener {
private final JLabel label = new JLabel();
private final JScrollPane scroll = new JScrollPane(label);
private final JViewport vport = scroll.getViewport();
private final URL url1 = null;
private final URL url2 = null;
Container c;
ButtonGroup bg;
JRadioButton but1,but2;
JPanel p ;

public MainPanel2(){

   c = this.getContentPane();
   c.setLayout(new BorderLayout(10,10));

   JPanel p = new JPanel();
   p.setLayout(newFlowLayout(FlowLayout.CENTER,10,10));
   p.setBackground(Color.white);


   c.add("North",p);
   c.add("Center",label);


  JRadioButton but1 = new JRadioButton("Image1",true);
  JRadioButton but2 = new JRadioButton("Image2",false);


ButtonGroup bg = new ButtonGroup();


bg.add(but1);
bg.add(but2);


p.add(but1);
p.add(but2);


but1.addActionListener(this);
but2.addActionListener(this);



HandScrollListener hand = new HandScrollListener();
vport.addMouseListener(hand);
vport.addMouseMotionListener(hand);
add(scroll);
scroll.setPreferredSize(new Dimension(400,300));

}
class HandScrollListener extends MouseInputAdapter {

    private final Cursor defCursor =   Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);

    private final Cursor hndCursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);

    private final Point pp = new Point();

public void mouseDragged(MouseEvent e){
Point cp = e.getPoint();
Point vp = vport.getViewPosition();
    vp.translate(pp.xcp.x,pp.ycp.y);
    label.scrollRectToVisible(newRectangle
    (vp,vport.getSize()));
pp.setLocation(cp);
}
public void mousePressed(MouseEvent e){
label.setCursor(hndCursor);
pp.setLocation(e.getPoint());
}
public void mouseReleased(MouseEvent e){
label.setCursor(defCursor);
label.repaint();
}
   }

public void actionPerformed(ActionEvent e) {
      URL url1 = null;
  URL url2 = null;

try {
url1= newFile("CRW_3857_JFR.jpg").toURI().toURL();
url2 = new File("1img_0561.jpg").toURI().toURL();
}
catch (MalformedURLException ex) {
ex.printStackTrace();
}

if( but1.isSelected()) {
label.setIcon(new ImageIcon(url1));
}
if( but2.isSelected() ) {
label.setIcon(new ImageIcon(url2));
}
}


public static void main(String[] args){
EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}



public static void createAndShowGUI(){
try{
UIManager.setLookAndFeel
       (UIManager.getSystemLookAndFeelClassName());
}
    catch(Exception e) {
e.printStackTrace();
}
JFrame frame = new JFrame("HandScroll");

    frame.setDefaultCloseOperation
        (WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(new MainPanel2());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
大変見にくくなっていますがどうかアドバイスお願いします。

A 回答 (1件)

JRadioButtonをコントラクター内でローカルに宣言しているので、アクションのメソッドないではNULLだといっていますね。



コンストラクターないのJRadioButtonを消せばもんだいないですよ。
    • good
    • 0
この回答へのお礼

解答ありがとうございます。
コンストラクター内のJRadioButtonを消したばあい、JRadioButtonの作成はどこで行えばいいのでしょうか?

お礼日時:2010/03/21 16:10

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!