我在一个jFrame中有2个jPanel,在第一个jPanel(Steiner)我绘制图形,在第二个B上,我有按钮,我在框架上添加到jPanel,按钮的所有动作都在Frame类中,I无法使用Steiner的公共方法。我怎么用呢?
package net.kxyz;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Random;
/**
*
* @author kuman
*/
public class Steiner extends javax.swing.JPanel {
int minX = 1000, minY = 1000, maxX = 0, maxY = 0;
long temp = 1000;
Graph graph = new Graph(6);
Point[] p = new Point[6];
int hight = 7; // hieight of oval - Point
int width = 7; // width of oval - Point
int terminals;
Random rand = new Random();
LazyPrimMinimumSpanningTree lazyPrimMST = new LazyPrimMinimumSpanningTree(graph);
/**
* Creates new form Steiner
*/
public Steiner() {
graph = setTerminals();
lazyPrimMST.Find(graph);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 317, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 279, Short.MAX_VALUE)
);
}// </editor-fold>
// Variables declaration - do not modify
// End of variables declaration
@Override
public void paint(Graphics x) {
super.paint(x);
Dimension d = getSize();
Graphics2D g = (Graphics2D) x;
for (int i = 0; i < 100; i++) {
g.setColor(Color.WHITE);
g.fillRect(0, 0, d.width, d.height);
findSolution();
for (Edge edge : lazyPrimMST.edges()) {
// g.setColor(Color.black);
g.setColor(edge.getPointP().getColor());
g.fillOval(edge.getPointP().getX(), edge.getPointP().getY(), width, hight);
g.setColor(edge.getPointN().getColor());
g.fillOval(edge.getPointN().getX(), edge.getPointN().getY(), width, hight);
g.drawLine(edge.getPointP().getX(), edge.getPointP().getY() + 4, edge.getPointN().getX(), edge.getPointN().getY() + 4);
}
}
}
public void findMinMax(Point p, Point n) {
//code
}
public void findSolution() {
//code
}
}
框
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package net.kxyz;
/**
*
* @author kuman
*/
public class Ramka extends javax.swing.JFrame {
/**
* Creates new form Ramka
*/
public Ramka() {
Steiner steiner;
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
steiner1 = new net.kxyz.Steiner();
b1 = new net.kxyz.B();
bRandom = new javax.swing.JButton();
bStart = new javax.swing.JButton();
valueOfTerminals = new javax.swing.JSpinner();
jLabel1 = new javax.swing.JLabel();
valueOfGeneration = new javax.swing.JSpinner();
valueOfLimfocits = new javax.swing.JSpinner();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jScrollPane2 = new javax.swing.JScrollPane();
textPanel = new javax.swing.JTextPane();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout steiner1Layout = new javax.swing.GroupLayout(steiner1);
steiner1.setLayout(steiner1Layout);
steiner1Layout.setHorizontalGroup(
steiner1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
steiner1Layout.setVerticalGroup(
steiner1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 251, Short.MAX_VALUE)
);
bRandom.setText("Losuj");
bRandom.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bRandomActionPerformed(evt);
}
});
bStart.setText("Start");
bStart.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bStartActionPerformed(evt);
}
});
valueOfTerminals.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
public void propertyChange(java.beans.PropertyChangeEvent evt) {
valueOfTerminalsPropertyChange(evt);
}
});
jLabel1.setText("Liczba terminali:");
jLabel2.setText("Liczba generacji:");
jLabel3.setText("Liczba limfocytów:");
jScrollPane2.setViewportView(textPanel);
javax.swing.GroupLayout b1Layout = new javax.swing.GroupLayout(b1);
b1.setLayout(b1Layout);
b1Layout.setHorizontalGroup(
b1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(b1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(b1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(b1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(b1Layout.createSequentialGroup()
.addComponent(valueOfLimfocits, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(bStart, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(b1Layout.createSequentialGroup()
.addComponent(valueOfTerminals, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(bRandom, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(valueOfGeneration, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE)
.addGap(11, 11, 11))
);
b1Layout.setVerticalGroup(
b1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(b1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(b1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane2)
.addGroup(b1Layout.createSequentialGroup()
.addGroup(b1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(bRandom)
.addComponent(valueOfTerminals, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(b1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(valueOfGeneration, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(b1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(valueOfLimfocits, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3)
.addComponent(bStart)))))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(steiner1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(b1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(steiner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(b1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void bStartActionPerformed(java.awt.event.ActionEvent evt) {
findSolution(); //doesnt work (?)
}
private void valueOfTerminalsPropertyChange(java.beans.PropertyChangeEvent evt) {
// TODO add your handling code here:
}
private void bRandomActionPerformed(java.awt.event.ActionEvent evt) {
findSolution();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/*
* Set the Nimbus look and feel
*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Ramka.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Ramka.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Ramka.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Ramka.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Ramka().setVisible(true);
}
});
}
// Variables declaration - do not modify
private net.kxyz.B b1;
private javax.swing.JButton bRandom;
private javax.swing.JButton bStart;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JScrollPane jScrollPane2;
private net.kxyz.Steiner steiner1;
private javax.swing.JTextPane textPanel;
private javax.swing.JSpinner valueOfGeneration;
private javax.swing.JSpinner valueOfLimfocits;
private javax.swing.JSpinner valueOfTerminals;
// End of variables declaration
}
乙
package net.kxyz;
public class B extends javax.swing.JPanel {
/**
* Creates new form B
*/
public B() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(22, 22, 22)
.addComponent(jButton1)
.addContainerGap(305, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(30, 30, 30)
.addComponent(jButton1)
.addContainerGap(247, Short.MAX_VALUE))
);
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
findSoluton(); // doesnt work too (?) // TODO add your handling code here:
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
// End of variables declaration
}
答案 0 :(得分:2)
您可以将Steiner实例传递给B实例,然后允许B在Steiner上调用公共方法。
如,
B.java(顺便说一下可怕的班级名称):
public class B {
private Steiner steiner;
public B(Steiner steiner) {
this.steiner = steiner;
}
Frame.java:
steiner1 = new net.kxyz.Steiner();
b1 = new net.kxyz.B(steiner1); // ***** note the change
有关您的代码的其他评论: