- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
感谢您帮我看这个,这是我的问题:
我的 MyFrame 类中的 If 语句说“MyShape 是抽象的,无法实例化”我也不知道如何完成我的 actionlistener,以便它创建一个我从 jmenu 选择的形状的实例。最后将我的 Jslider 的尺寸设置为我的 square 的大小和面积,并将其添加到 JtextField。
我很乐意提供任何帮助,如果你能解释我需要做的事情,这是一种简单的方法,让像我这样的编程新手能够理解 :)
我的框架
package assignment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class MyFrame extends javax.swing.JFrame {
public MyFrame() {
// Create the menu
JMenuBar topMenu = new JMenuBar();
this.setJMenuBar(topMenu);
//create the menu button "shapes"
JMenu shapes = new JMenu("Shapes");
topMenu.add(shapes);
//Create the 3 shapes for the menu
JMenuItem square = new JMenuItem("Square");
JMenuItem circle = new JMenuItem("Circle");
JMenuItem triangle = new JMenuItem("Triangle");
//add shapes to menu
shapes.add(circle);
shapes.add(triangle);
shapes.add(square);
//add the menu
setJMenuBar(topMenu);
MyControlPanel pane = new MyControlPanel();
this.add(pane);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
/**
* @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(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MyFrame().setVisible(true);
}
});
class squareAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
JMenuItem clickedMenu = (JMenuItem) e.getSource();
if (clickedMenu.getText().equals("Square")) {
MyShape ASquare = new MyShape() {
};
} else if (clickedMenu.getText().equals("Circle")) {
MyShape Circle = new MyShape();
} else if (clickedMenu.getText().equals("Triangle")) {
MyShape Triangle = new MyShape();
}
}
}
}
}
package assignment;
//import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class MyControlPanel extends javax.swing.JPanel {
JSlider slider;
JLabel sliderLabel;
JLabel sliderdimension;
JLabel blank;
JLabel dl;
JLabel area1;
/**
* Creates new form MyControlPanel
*/
public MyControlPanel() {
slider = new JSlider();
slider.setValue(50);
slider.addChangeListener(new MyChangeAction());
slider.setMajorTickSpacing(10);
slider.setPaintLabels(true);
slider.setPaintTicks(true);
slider.setBounds(300, 50, 100, 50);
sliderLabel = new JLabel("50");
blank = new JLabel(" ");
sliderdimension = new JLabel("Shape Dimension:");
JTextField boundary_length = new JTextField("Boundary Length");
JTextField area = new JTextField("Area");
dl = new JLabel("Boundary Length =");
area1 = new JLabel("Area =");
setLayout(new BorderLayout());
JPanel sliderPanel = new JPanel();
sliderPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 0));
sliderPanel.add(sliderdimension);
sliderPanel.add(sliderLabel);
sliderPanel.add(slider);
sliderPanel.add(dl);
sliderPanel.add(boundary_length);
sliderPanel.add(area1);
sliderPanel.add(area);
this.add(sliderPanel, BorderLayout.PAGE_END);
}
/**
* 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.
*/
// <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, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
}// </editor-fold>
// Variables declaration - do not modify
// End of variables declaration
public class MyChangeAction implements ChangeListener {
//complete code here
public void stateChanged(ChangeEvent ce) {
int value = slider.getValue();
String str = Integer.toString(value);
sliderLabel.setText(str);
}
} // end class
}
我的形状
package assignment;
public abstract class MyShape
{
double thelength;
double thearea;
public abstract double computeBoundaryLength(double Length);
public abstract double computeArea (double Length);
}
正方形
package assignment;
public class ASquare extends MyShape {
@Override
public double computeBoundaryLength(double Length)
{
thelength=(4*Length);
return thelength;
}
@Override
public double computeArea(double Length)
{
thearea=(Length*Length);
return thearea;
}
}
类 shapeAction 实现 ActionListener{ public void actionPerformed(ActionEvent e){
JMenuItem clickedMenu = (JMenuItem)e.getSource();
if (clickedMenu.getText().equals("Square")){
//implement abstract methods
MyShape ASquare = new ASquare();
ASquare.addActionListener(new shapeAction());
}
else if (clickedMenu.getText().equals("Circle")){
//implement abstract methods
MyShape ACircle = new ACircle();
ACircle.addActionListener(new shapeAction());
}
else if (clickedMenu.getText().equals("Triangle")){
//implement abstract methods
MyShape ATriangle = new ATriangle();
ATriangle.addActionListener(new shapeAction());
最佳答案
squareAction
是用来监听所有可能的菜单选择的类(每种形状一个),所以它应该被命名为 ShapeMenuAction
或其他名字像那样。如果您希望在选择菜单选项时执行 ShapeMenuAction
的 actionPerformed
,您必须创建它的一个实例并将其添加到每个 MenuItem
对它们使用 addActionListener
方法。否则,什么也不会发生。在 MyFrame 中:
JMenuItem square = new JMenuItem("Square");
square.addActionListener(new ShapeAction());
JMenuItem circle = new JMenuItem("Circle");
circle.addActionListener(new ShapeAction());
JMenuItem triangle = new JMenuItem("Triangle");
triangle.addActionListener(new ShapeAction());
actionPerformed
中,您必须实例化MyShape
的具体子类。现在你除了实例化一个形状什么都不做,你必须在方法结束之前对对象做一些事情,对吧?在您的 MyControlPanel
中创建一个 setShape
方法,并从 actionPerformed 调用它,将新创建的形状传递给它。在 ShapeAction 中:
public void actionPerformed(ActionEvent e) {
JMenuItem clickedMenu = (JMenuItem) e.getSource();
if (clickedMenu.getText().equals("Square")) {
MyShape aSquare = new ASquare();
myControlPanel.setShape(aSquare);
} else if (clickedMenu.getText().equals("Circle")) {
MyShape aCircle = new ACircle();
myControlPanel.setShape(aCircle);
} else if (clickedMenu.getText().equals("Triangle")) {
MyShape aTriangle = new MyShape();
myControlPanel.setShape(aTriangle);
}
}
您必须在 MyFrame 中创建一个名为 myControlPanel 的 MyControPanel:
private MyControlPanel myControlPanel
并添加它
myControlPanel = new MyControlPanel();
getContentPane().add(myControlPanel);
之前
pack();
在 MyControlPanel
中,您现在可以根据 slider 值和所选形状更新标签,每次其中一个发生变化时。
私有(private) MyShape 形状;public void set Shape(MyShape shape) { this.shape = 形状; 更新标签();
关于Java,听众建议,swing comp's,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13016684/
这个问题在这里已经有了答案: Callback or Promise? [duplicate] (2 个答案) 关闭 7 年前。 我是一个巨大的 promise 链,但是我意识到其中一个函数返回一个
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: “On file dialog cancel” event in JavaScript 当用户点击文件上传对
我还在学习 flutter 。以下是本教程中的代码。当我调用Authservice配置文件时,它返回Observable>。如何获取其中的数据以进行显示。 这是我到目前为止所做的事情,但是它返回了整个
$(".cblClaimSources").on('click', ':checkbox', function (){ alert('im : '+ $(this).attr('id') +
首先,我知道这个问题已经被问过多次,但我的应用程序最近出现的问题激起了我对这个问题的兴趣。我也阅读了 Firebase 文档。 What is the difference between Child
由于 Tomcat 8.0 已停产,我想将我的应用程序直接升级到 Tomcat 9。 当我将基于 Java 的应用程序从本地部署到我的服务器时,我有一个脚本覆盖了 server.xml。我将服务器升级
我是一名优秀的程序员,十分优秀!