我正在尝试用Java创建一个SpringLayout,这是我的代码(从Oracle Java文档中获取)
import javax.swing.*;
import java.awt.*;
public class SpringForm {
private static void createAndShowGUI() {
String[] labels = {"Side1: ", "Side2: ", "Side3: "};
int numPairs = labels.length;
//Create and populate the panel.
JPanel p = new JPanel(new SpringLayout());
javax.swing.JButton calculate_btn;
calculate_btn = new javax.swing.JButton();
for (int i = 0; i < numPairs; i++) {
JLabel l = new JLabel(labels[i], JLabel.TRAILING);
p.add(l);
JTextField textField = new JTextField(10);
l.setLabelFor(textField);
p.add(textField);
}
calculate_btn.setText("Calculate");
p.add(calculate_btn);
//Lay out the panel.
SpringUtilities.makeCompactGrid(p,
numPairs, 2, //rows, …
Run Code Online (Sandbox Code Playgroud) 我试图在java中使用GRIDBAG布局实现此布局
public static void addComponentsToPane(Container pane) {
if (RIGHT_TO_LEFT) {
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
JLabel label1,label2,label3,result,title;
JButton calculate_btn;
JTextField side1,side2,side3;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
if (shouldFill) {
//natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;
}
title = new JLabel("Area of Triangle");
if (shouldWeightX) {
c.weightx = 0.5;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = -1;
pane.add(title, c);
label1 = new JLabel("Side 1: ");
if (shouldWeightX) {
c.weightx = 0.5;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 20;
c.gridx …
Run Code Online (Sandbox Code Playgroud) 这个问题真的令人困惑
A ________
是一种与该类同名的特殊方法,只要实例化该类的对象,就会自动调用该方法.回答:
constructor
二传手
吸气
静态方法
我在想构造函数是唯一与类同名的构造函数,但是等等!构造函数实际上不是一种方法,它与方法不同.所以我读了这篇文章并得出结论,这个问题格式错误,我是对的吗?
我已经查看了其他问题但不完全是我在这里遇到的问题,这是我现有的代码
public class Qn3 {
public static void displayHighestMark(String[] names,int[] marks, int count)
{
int mark = 0;
int currentArrayPosition;
for(int i=0;i <= names.length;i++)
{
if(mark <= marks[i])
mark = marks[i];
currentArrayPosition = i;
}
System.out.println(name[i]+" with marks "+mark);
}
public static void main(String[] args)
{
String[] names = new String[]{"jack","hello","A","b","c","d"};// = new String[];
int[] marks = new int[]{1,2,3,8,5,6};
displayHighestMark(names,marks, 45);
}
}
Run Code Online (Sandbox Code Playgroud)
所以基本上我试图找到标记数组中的最高标记.但我得到了ArrayIndexOutOfBoundsException的预期
在java中如何在方法中使用类变量?
这是我的代码
public class ExamQ3a {
String[] descriptionArr = new String[50];
static int[] codeArr = new int[50];
public static void displayItems(int count, int[] codeArr,
String[] descriptionArr) {
count = this.codeArr.length;
for (int i = codeArr.length; i < codeArr.length; i--) {
}
}
}
Run Code Online (Sandbox Code Playgroud)
这里突出显示的行是count = this.codeArr.length; 我得到的错误是无法从静态上下文引用非静态变量.但我已经将变量设为静态.什么给出了什么?
所以只按要求!不是我想问整个问题,只是为了知道我为什么要使用静态,这是一个练习题
您将开发一个简单的应用程序系统来管理公司的库存.系统应该能够维护最多50个项目的列表.每个项目都有唯一的整数代码和描述.
(a)编写声明和创建两个数组的Java语句来存储代码和项的描述.
(b)使用以下方法签名编写Java方法:
public static void displayItems(int count,int [] codeArr,String [] descriptionArr)
此方法以表格形式显示公司中所有项目的代码和说明,并带有相应的列标题.
参数:codeArr:存储项目代码的数组
descriptionArr:存储项目描述的数组
count:系统中的项目数
我有这段代码,根据这个页面在这里 下面的输出应该是正确的给我,98.24但是这给了我68.8,我在这里缺少什么?
public class Qn1
{
public static void main(String[] args)
{
double cel = 36.8;
double fah = ((9 / 5 )* cel) + 32;
System.out.println(cel + "deg C =" + fah +" deg F");
}
}
Run Code Online (Sandbox Code Playgroud) 我在我的catch语句中用红色下划线,不确定我得到的错误是什么"永远不会在相应的try语句中抛出"这是什么意思?一切看起来都很好.
public void validateTriangle(int sidea, int sideb, int sidec) throws InvalidValueException {
try {
if ((sidea + sideb > sidec) || (sideb + sidec > sidea) || (sidea + sidec > sideb)) {
findArea(side1, side2, side3);
}
} catch (InvalidValueException excep) {
message = excep.getMessage();
}
}
Run Code Online (Sandbox Code Playgroud)