小编Cur*_*ore的帖子

Java问题使用字符串参数实例化对象

我正在尝试执行我的任务,但我遇到一个实例化具有String参数的对象的问题.当我编译并运行我到目前为止的应用程序时,它返回String值"Null"而不是我期望它.

这是我的抽象超类

public abstract class Book
{
//Declaration of class variable
private String title;
protected double price;

// contructor for Book class objects
public Book(String bookTitle)
    {
        bookTitle = title;
    }
//method that gets and returns books title
public String getTitle()
    {
        return title;
    }
//method that gets and returns books price
public double getPrice()
    {
        return price;
    }
//abstract method with no parameters
public abstract void setPrice();
}
Run Code Online (Sandbox Code Playgroud)

这是我的子类

public class Fiction extends Book
{
//subclass contructor
public Fiction(String …
Run Code Online (Sandbox Code Playgroud)

java string instantiation

1
推荐指数
1
解决办法
590
查看次数

如何在其他方法中使用main方法的变量(用户输入)?

我正在尝试为课程做一个作业,但我遇到了一个我无法找到解决方案的问题.我的main方法中有一个名为passwd的变量.我有一个用户输入一个可能的密码,该输入存储在变量中.然后我检查密码变量的长度,以确保它满足长度要求.我想有另一个方法聊天检查变量的每个字符,看看它是否是一个数字.

问题是我无法在我的digitCheck()方法中使用main方法中的passwd变量.

有人可以告诉我如何解决这个问题.

 package Password;

import java.awt.Component;
import javax.swing.JOptionPane;

/**
 *
 * @author Curtis
 */
public class Password 
{
private static Component frame;

//Main Method
public static void main(String[] args) 
{//Declaration of variables
    String passwd;
    int leng;
    boolean length = false;
    //Prompt user to enter possible password
    while(!length)
    {
        passwd = JOptionPane.showInputDialog("Please enter a possible password:\n" +  
            "Password must contain 6-10 characters\n"+
            "Password must contain both a letter and a digit");
        leng =passwd.length();//Determines Password Length
        if(leng>5 && leng<11)
            { 
                length …
Run Code Online (Sandbox Code Playgroud)

java variables methods

0
推荐指数
1
解决办法
7491
查看次数

Java:自定义图标问题

我想把一个自定义图标放到我的身上JFrame.我的项目文件夹中有图标图像,但我似乎无法使其工作.

我也试过,setIconImage(new ImageIcon(imgURL).getImage());但似乎对我也没有用.

此外,自定义图标的典型尺寸是多少

import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

/**
* @author Curtis
*/
public class Favorites extends JFrame implements ActionListener
{
String[] styles = {"Big Band", "Country", "Pop", "Rock", "Rap"};
Font boxFont = new Font("Times New Roman", Font.BOLD, 14);
JLabel instruct = new JLabel("What is your favorite type of music?");
JComboBox music = new JComboBox(styles);
JTextField result = new JTextField(20); …
Run Code Online (Sandbox Code Playgroud)

java swing class jframe

0
推荐指数
1
解决办法
194
查看次数

标签 统计

java ×3

class ×1

instantiation ×1

jframe ×1

methods ×1

string ×1

swing ×1

variables ×1