小编Bod*_*yte的帖子

如何动态更改JLabel

我有一个JLabel和一个按钮,JLabel显示按钮被按下的次数,但是,我不知道如何更新JLabel显示按下按钮的次数.

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class SimpleGui {
   private JFrame f = new JFrame("Basic GUI"); // create Frame
   int pressed = 0; // tracks number of button presses.
   JLabel label1 = new JLabel("You have pressed button " + pressed + "times.");
   private JButton start = new JButton("Click To Start!");

   public SimpleGui() {
      // Setup Main Frame
      f.getContentPane().setLayout(new GridLayout(0, 1));
      start.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            calculate();
         }
      });
      // …
Run Code Online (Sandbox Code Playgroud)

java swing jlabel

6
推荐指数
1
解决办法
3万
查看次数

Java:如何从文件中读取文本和数字

我正在创建一个简单的程序来从文本文件中读取数据.该文件存储有关人员的信息,每行包含姓名,年龄和编号:

例如:每行的文件格式

      Francis Bacon  50    2
Run Code Online (Sandbox Code Playgroud)

如果它只是文本,我可以在文件中读取没有问题,但我对如何区分文本和数字感到困惑.这是我的代码:

import java.io.*;


public class Test{

    private People people[] = new People[5];


    public Test(){
        BufferedReader input;

        input = new BufferedReader(new FileReader("People.txt"));// file to be readfrom
        String fileLine;
        int i = 0;

        while (test != null){
            fileLine = input.readLine(); 

            // Confused as to how to parse this line into seperate parts and store in object:
            // eg:
            people[i].addName(fileLine - part 1);
            people[i].addBookNo(fileLine - part 2);
            people[i].addRating(fileLine - part 3)

            i++

        }

    }

}
Run Code Online (Sandbox Code Playgroud)

java file input

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

Android:启动onListViewItem上的新活动

我有一个包含基本Listview的Activity.功能如下:

单击列表视图项 - >获取列表视图项名称 - >按意图传递名称并开始新活动.

除了新活动的开始之外,一切正常.它只是没有响应,我不明白为什么.我真的很感激一双额外的眼睛来看看这个.

listView = getListView();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, foodNames);
listView.setAdapter(adapter);

listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView parentView, View childView, int position, long id) {

        TextView tv  = (TextView) listView.getSelectedItem();
        String value = tv.getText().toString();

        Log.v("DEBUG","Name of item clicked" + value);

        Intent intent = new Intent(childView.getContext(), FoodItemActivity.class);
        intent.putExtra("FoodName", value );
        startActivity(intent);

    }

    public void onNothingSelected(AdapterView parentView) {
    }
});
Run Code Online (Sandbox Code Playgroud)

android listview android-intent android-activity

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